Svedr Svedr - 8 months ago 71
PHP Question

Translate dynamic value/string from database [Twig, Laravel]

I am using the October CMS ( that is based on the Laravel Framework for my web app.


First I am calling an external server and getting an XML array which I translate and insert into my local database. Then I pull these values from my local database and try to display them on the front-end. The issue is, that I have 2 languages that I need to cater to.


{% if activeLocale == "si" %}
{{ record.estate_type_SI|raw }}
{% elseif activeLocale == "en" %}
{{ record.estate_type_EN|raw }}
{% endif %}

This works, but when I have multiple items it gets gruesome because I have to write everything down two times. What this does is that depending on the language a value from a different column in the database is pulled.

I am wondering if I can do something similar to this:

{{ record.estate_type_{{"SI"|trans}}|raw }}

I will gladly buy you a beer if you can help me out with this.

EDIT: Variables
enter image description here


Using attribute , you can access a property of an object in a dynamic way. Then you just have to use upper filter to match what you need.

{{ attribute(record, 'estate_type_'~ activeLocale|upper)|raw }}