Score:-1

Custom translation function

ma flag

I need own translation function in my custom module where i have defined language codes ('en','ja','de'....etc) Now i just wanted to pass word and language code as a parameter and it returns translation of that word according to language code.

I am trying this inside my translation but it doesn't work it is returning 'yes' instead i don't know why.

 $japanese_value = t('yes', array(), array('langcode' => 'ja')); 
 print_r($japanese_value);

And please let me know if there is a better way to do this or any other translation module.

Jaypan avatar
de flag
That looks to me as if it should work. Have you translated the value of _yes_ to _はい_ in your system?
Score:0
us flag

Requiring t() to translate the string in a specific language is perfectly fine. The function will return a translation, if the Locale module is installed and enabled, and that string is present in the site; it doesn't use a remote service to translate the string passed as first argument.

Present in the site means:

  • The translation has been added using the form on admin/config/regional/translate

  • The translation has been imported using the form on admin/config/regional/translate/import

  • The settings.php file contains a custom translation in the $conf['locale_custom_strings_ja'] array (for Japanese). For example, the following array would allow to get the Italian translation for Forum and node. (I used Italian just because I know how to translated those words.)

    $conf['locale_custom_strings_it'][''] = array(
      'Forum' => 'Bacheca discussioni',
      'node' => 'contenuto',
    );
    

Note that in the latter case, the Local module doesn't need to be enabled.

Rog Boy avatar
ma flag
thankyou, I wanted to ask you if i add translation in content type -> field ( list (yes,no)) -> edit -> translate ..... is it possible to call some functions to get a translation from there... ?
apaderno avatar
us flag
Do you mean from `$conf['locale_custom_strings_ja']`? That is automatically done from Drupal.
Rog Boy avatar
ma flag
admin/config/regional/translate/translate (here i can see the translation of yes , no )....... now i have this value in form array with a field_name and value (yes)............I am creating reports by my custom module i just wanted to translate before generating PDF......... .....so i am tring to create function where i can pass that field name and value to get translation in return ..
Rog Boy avatar
ma flag
would be very thankful i m new and stuck in this task from friday
apaderno avatar
us flag
So you are trying to translate the content of a field, not a static string like `'yes'`. Retrieve the value of that field, and pass it to `t()`. If the value of that field can be any value users enter, than it won't work, anyway, as you would need to add as translation any possible value users could enter. Keep in mind that `t()` can only give you a translated string if the translation of the full string is present. Given, for example, *Yes, I know that.* Drupal would not retrieve the translation for *Yes*, *I*, *know*, and *that*.
apaderno avatar
us flag
Anyway, this is a different question that should be posted as new question. It also requires more details to be answered, and a back-and-forth on comments is not the ideal to answer to questions.
Rog Boy avatar
ma flag
https://drupal.stackexchange.com/questions/304164/get-translation-programmatically-of-field-value-in-specific-language
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.