
By majorrobot on
So, the t() function is great for translating a string from the default language into a different language. If a multilingual site's default language is English, and we're viewing it in Spanish, this:
t('dog');
will give us "perro."
But if we're viewing the site in Spanish, and we have the string "perro," it appears that we cannot translate back to the default language string, "dog." This seems to happen even if we pass t() a langcode:
t('perro', array(), array('langcode' => 'en'));
That, frustratingly, also returns "perro."
So, I'm wondering if (a) I'm correct or not about this, and (b) if anyone knows a good method to translate back to the default language?
Thanks in advance!
Comments
If you use t('dog'), it will
If you use
t('dog')
, it will always display in the current language (if a translation has been entered for the current language). So it's not clear why you would need to translate back. Can you give more information on this?Contact me to contract me for D7 -> D10 migrations.
Use t('dog')
Just simply use t('dog').
It will always display translation for the current language (if a translation has been entered for the current language).
Thanks for your responses!
Thanks for your responses! Yes, t('dog') works perfectly if I need to translate to the current language. But there may be cases where, in code, I need to translate back to the default language.
Here's a simplified example. Say I have a path www.example.com/dog. The translation of this content is at www.example.com/perro.
The code in my module needs to know whether this is a "dog" page or not, no matter what language we're in. Say we're doing this:
If we're viewing the page in Spanish, then arg(0) does not equal "dog" anymore, and the code inside the conditional is not executed. I need a way to see if arg(0) equals "dog" in the default language, no matter what language I'm viewing the site in — so the code inside the conditional executes. I had hoped that this would work:
But, in this case, when viewing in Spanish, $translated_arg will be "perro" instead of "dog" since t() doesn't like translating back to the default language.
Is there a better way to do this, as t() does not achieve what I'm looking for?
Thanks!
I don't believe Drupal has a
I don't believe Drupal has a way to translate back like you want. The best you could do is:
Contact me to contract me for D7 -> D10 migrations.
Thanks, jaypan. I think you
Thanks, jaypan. I think you're right.
I've decided to get around the problem by creating a custom function that wraps t(). It needs more work (specifically it really needs to incorporate caching), but I'll paste it here in case anyone else runs into the same issue:
Thanks again!
Thanks for this
Thanks majorrobot -- I ran into the same issue and thought I must be missing something, but as far as I can tell you're absolutely right, you can't get a string translated into the default language.
For my purposes it seemed to make more sense to use an alternative to locale() rather than t(), so here's a slightly modified approach (but using essentially the same code to accomplish the same thing):
TranslatableMarkup::getUntranslatedString
Struggled with this for a bit today, then found this method:
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21StringTranslation%21TranslatableMarkup.php/function/TranslatableMarkup%3A%3AgetUntranslatedString/8.8.x