Search
Other ReReplacer questions
Forum

Is it possible to use ReReplacer for multilingual purposes?

Rikard Jespersen's Avatar Rikard Jespersen
I wonder if it is possible to have different replacements based on the active site language?
For example, I have a multilingual site and I am looking for a way to replace language constants. The native joomla language override system doesn't work for all parts of my site with my template (YooTheme Pro). But I want to set the site up, so that if YooTheme implements the functionality in the future, the contants already inserted will be usable with the native language override system.

As an example, I would like to insert the constant "MYFIELD_ONE_PLAYER" and then when the site (html lang="") is:
sv-SE = En spelare
en-GB = Single player
nb-NO = Enspiller
da-DK = Èn spiller
fi-FI = Yksinpeli

and so on.

I am using Joomla 4.0.5 and ReReplacer v12.1.0 [PRO]

EDIT:
I have tried this:

##Search##
MYFIELD_(\d+)_PLAYERS

##Replace##
<?php 
$currentLanguage = JFactory::getLanguage();
$currentLanguageTag = $currentLanguage->get('tag');
?>

<?php if($currentLanguageTag=='en-GB'): ?>
Up to \1 players
<?php elseif($currentLanguageTag=='sv-SE'): ?>
Upp till \1 spelare
<?php elseif($currentLanguageTag=='nb-NO'): ?>
Optil \1 spillere
<?php elseif($currentLanguageTag=='da-DK'): ?>
Op til \1 spillere
<?php elseif($currentLanguageTag=='fi-FI'): ?>
Jopa \1 pelaajaa
<?php endif; ?>

This seems to work well, but I don't know if this is the correct approach. If it can be done in an easier/better way. Perhaps the code can/should be written in some other way that is more efficient.
Peter van Westen's Avatar Peter van Westen ADMIN
It is easier to just use language strings in your content.

Place something like this in your content:
{text MYFIELD_UP_TO_ONE_PLAYER}

Then in ReReplacer, search for:
\{text (.*?)\}
(using Regular Expressions)
And replace with PHP code:
<?php echo JText::_('\1'); ?>

You could also pass multiple language tags to it so it uses sprintf to 'combine' them.
Like:
{text MYFIELD_UP_TO,MYFIELD_ONE_PLAYER}
Then in your language files or overrides have:
MYFIELD_UP_TO="Up to %s"
MYFIELD_ONE_PLAYER="Single player"
MYFIELD_UP_TO="Upp till %s"
MYFIELD_ONE_PLAYER="En spelare"
etc...
Please post a rating at the Joomla! Extensions Directory
Rikard Jespersen's Avatar Rikard Jespersen
Thank you Peter
That is a very nice looking solution. I don't think I can use it for my particular situation though, because of how I am retrieving the data. It might be that I am mistaken, but I'll explain further.

I have a custom field called "Number of players" (type: list). This custom field have X number of items in it. I have set the name for each of the items to:
MYFIELD_ONE_PLAYER
MYFIELD_TWO_PLAYERS
MYFIELD_THE_PLAYERS
Etcetera

I use this data in my articles by choosing the appropriate number of players in the custom field drop down. For example "up to 2 players" (I have made language overrides for all these in the administrator and site so that I can see the correct string. It works everywhere on the site, except through yootheme)

In Yootheme Pro I use dynamic data to fetch the data from the article and it is this output I want to localize. Yootheme does not resolve language strings and that is why I need to replace the output.
Peter van Westen's Avatar Peter van Westen ADMIN
Whatever works for you...
Please post a rating at the Joomla! Extensions Directory
Rikard Jespersen's Avatar Rikard Jespersen
Wow, my bad. I didn't understand your instructions. But I tried it out and anyway and it works like a charm. I did change it a little bit though because of my setup. And that I want it to be ready for if Yootheme decides to resolve language strings in the future. That is, I don't use {} in the name field. I just have MYFIELD_VALUE_ONE_PLAYER there. That way it will be translated via the native joomla functionality and I don't need to enable "Admin" as a search area.

Search
MYFIELD_VALUE_(\d+)_PLAYERS
MYFIELD_VALUE_ONE_PLAYER

Replace
<?php echo JText::_('MYFIELD_\1_PLAYERS'); ?>
<?php echo JText::_('MYFIELD_VALUE_ONE_PLAYER'); ?>

Anyway. excellent solution and excellent support. Thank you!
Peter van Westen's Avatar Peter van Westen ADMIN
Nice work. 👍
Please post a rating at the Joomla! Extensions Directory
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription