We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Replace variables with custom one

How Do I?

vw000's avatar
vw000
241 posts
4 years ago
vw000's avatar vw000

I’m thinking of using the build in variables for language files in templates.

Example I create a 2 variables:

{en-hello} for English {de-hello} for German {fr-hello} for French

Is there a way to replace the language with a custom prefix based on the user logged in language?

Example, if the user language is DE, I set a custom locale variable to “de” and then it automatically replaces it on the template.

Then maybe hard coded in the templates something like:

{{lang}-hello}

Is this possible?

The idea is to replace the prefix automatically in the best performance way. I could just change the variables with if else statements on each template based on the user logged in language.

Note that I can get {lang} from the custom profile data already. But it will just print it instead of replace it.

I had this with an add-on before and it worked but just curious if there is another way to render the {lang} inside the variable natively.

UPDATE: I had an error in my testing.

This actually does work natively 😁😁😁😁 and you don’t even need to call the custom profile variables on templates, either. It works directly. Its nice when you play around sometime and things just work as expected by trying.

       
vw000's avatar
vw000
241 posts
4 years ago
vw000's avatar vw000

UPDATE 2:

I seems {lang} is not the actual user lang but the site. You need to use {language} instead. Someone might correct me on this, I would of course prefer to use the shorter locale tag.

The only annoying thing about this approach is that {language} is not rendered outside {exp:member:custom_profile_data} which means you need to put all your template content inside those tags. Not an ideal approach, I guess.

This solution would work great if you could actually have the user language as some global variable that is rendered directly on templates or anywhere.

       
jelle's avatar
jelle
40 posts
4 years ago
jelle's avatar jelle

You could also use Low Variables for this! or another addon such as transcribe or publisher They have some added benefits too

       
vw000's avatar
vw000
241 posts
4 years ago
vw000's avatar vw000

That is precisely what I’m trying to avoid here. I used the add-on Republic Variables which did this, but it’s not compatible anymore.

I want to do this without any add-ons.

I think the solution here is to assign a custom global variable that you can call from anywhere in the templates.

I’m pretty sure that is more or less what probably my add-on was doing with a hook, but I have no problem hard coding this on the configuration file.

I just searched 2 hours and cannot find any proper instructions on how to create customer global variables to test this out sadly.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.