2

Is there a way to translate a single string with a language that is different from the default language?

(Google search did not lead me to a solution.) Example code that does not work:

// This shows in the default joomla language "en-GB"
echo JText::_('COM_COMPONENT_TEXT');

// Now I load the second language into an language object
$lang = JLanguage::getInstance('af-ZA');

// Now I translate the below string with "af-ZA"
echo $lang->_('COM_COMPONENT_TRANSLATEDTEXT');

// Now the third text is again in default language "en-GB"
echo JText::_('COM_COMPONENT_TEXT2');

I created the .ini files needed for the af-ZA translation. The below files were created for the task.

components/com_component/language/af-ZA/af-ZA.com_component.ini
components/com_component/language/en-GB/en-GB.com_component.ini

In my tests, I loaded the af-ZA language pack for Joomla. The JText translates correctly if I change my user profile to af-ZA, but then everything is translated to af-ZA -- not just the string I want translated.

My hope is to find a reliable Joomla technique that uses best practice. If it is not possible, I will write my own translation code.

Some context:

I am building a component behind login on the front part of a client Joomla website. The logged in user can choose a language on an activity that will generate a unique URL with content; the unique URL must show the content in the correct language. The viewer of the content (receiver of the url) won't be logged in for me to get the user language. The rest of the website stays in the default language. I just want the content to be in the language specified in the url.

6
  • So, you want to perform "one-off" / inline language translations for strings instead of setting one master language declaration to control the entire output? Correct? As pseudo code, you are hoping for something like echo JText::_('COM_COMPONENT_TEXT', 'af-ZA'); and then a subsequent line in the same script that is written: echo JText::_('COM_COMPONENT_TEXT', 'en-GB'); ? Just trying to clarify your task. Commented May 25, 2019 at 23:05
  • Yes you are correct.
    – Niel Buys
    Commented May 26, 2019 at 7:55
  • Can you add some realistic context to your question? I am wondering how "scalable" the solution needs to be. Are you only ever going to be translating a single, specific bit of text? or will this grow to accommodate a large pool of text values? I am assuming that you have already considered manually storing the off-language's translation in the original language's ini file like COM_COMPONENT_TEXT2_EN-GB. Commented May 26, 2019 at 9:02
  • @mickmackusa I have added real world context to the question.
    – Niel Buys
    Commented Jun 11, 2019 at 9:58
  • Maybe you don't need to over complicate the url generation step. I next to no experience with deploying a multidimensional Joomla site, but the url itself doesn't need to be translated, it just needs to contain the desired language code to be received by your site when the guest user arrives at your site? Is this right? Commented Jun 11, 2019 at 11:03

1 Answer 1

2

There is an alt method of the Text class that appears to allow you display translations in another language, https://api.joomla.org/cms-3/classes/Joomla.CMS.Language.Text.html

The example is not overly , or barely, explanatory enough for me but the description indicates it will use the same Constant and translate it into a different languages. Looking at the class itself doesn't really make it any clearer to me either, maybe someone else can explain how it works or if it might be useful.

/**
 * Translates a string into the current language.
 *
 * Examples:
 * `<?php echo Text::alt('JALL', 'language'); ?>` will generate a 'All' string in English but a "Toutes" string in French
 * `<?php echo Text::alt('JALL', 'module'); ?>` will generate a 'All' string in English but a "Tous" string in French
 *
 * @param   string   $string                The string to translate.
 * @param   string   $alt                   The alternate option for global string
 * @param   mixed    $jsSafe                Boolean: Make the result javascript safe.
 * @param   boolean  $interpretBackSlashes  To interpret backslashes (\\=\, \n=carriage return, \t=tabulation)
 * @param   boolean  $script                To indicate that the string will be pushed in the javascript language store
 *
 * @return  string  The translated string or the key if $script is true
 *
 * @since   1.7.0
 */
public static function alt($string, $alt, $jsSafe = false, $interpretBackSlashes = true, $script = false)
{
    if (Factory::getLanguage()->hasKey($string . '_' . $alt))
    {
        $string .= '_' . $alt;
    }

    return static::_($string, $jsSafe, $interpretBackSlashes, $script);
}

I couldn't find any examples or other references to it being used, but maybe it can be of use to you.

... a day later....

I have played with the Alt option to try and figure out how it works and this is what I have come up with.

In your Language .ini or an Override you set your Constants, e.g.

COM_FRED_GREETING='Hello'
COM_FRED_GREETING_BARNEY='Yo, Buddy'

In your extension you change your text to translate code from the default underscore to 'alt'

echo Text::_('COM_FRED_GREETING');

to

echo Text::alt('COM_FRED_GREETING',$some_var);

If you set $some_var = 'Wilma' you use the default translate and the output is 'Hello' because it couldn't find a Key to translate called COM_FRED_GREETING_WILMA=''

If you set $some_var = 'Barney' then it gets concatenated to the Constant as COM_FRED_GREETING_BARNEY and it matches a Key and the String is translated to 'Yo, Buddy'

So for the original question they could use something like;

COM_CUSTOM_TEXT='Good Morning'
COM_CUSTOM_TEXT_ZA='Mwauka bwanji'

and set

$alt = ''
echo Text::alt('COM_CUSTOM_TEXT',$alt);
$alt = 'za'
echo Text::alt('COM_CUSTOM_TEXT',$alt);
6
  • This looks like the start of a good/correct answer. If you would like to invite/encourage other volunteers to edit the post in an effort to fortify the guidance with additional explanation and implementation, you may decide to convert it to a wiki post. meta.stackexchange.com/q/11740/352329 You are under no obligation to do so, this strips the possibility of earning points, and once the post is declared a wiki there is no turning back. Either way, thank you for the contribution. Commented May 26, 2019 at 10:55
  • @mickmackusa thanks for the suggestion on turning it into a wiki post, made me read up on them, although I might leave it as it is for now and see if it answers the original question first. It seems such an obscure option and I couldn't find any other references to it, I could well be the only person to ever venture to look at it. :)
    – Irata
    Commented May 27, 2019 at 23:04
  • (I wouldn't convert to wiki while rep points earned will unlock more privileges for you. I just wanted to throw it out there.) Good contributing. Keep it up. Commented May 27, 2019 at 23:26
  • Thanks I am a bit busy at the moment, will test this as soon as I can. This looks promising.
    – Niel Buys
    Commented Jun 11, 2019 at 9:43
  • One snag about the alt method I need to mention on testing the code. The Language ini part must be uppercase "COM_COMPONENT_SEARCH_af-ZA="Soek"" will not work. It needs to be uppercase like this ""COM_COMPONENT_SEARCH_AF-ZA="Soek""". The code part can be any case "JText::alt('COM_COMPONENT_SEARCH', 'af-ZA')". Thanks I am accepting answer.
    – Niel Buys
    Commented Jun 15, 2019 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.