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')); 

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

  • 2
    That looks to me as if it should work. Have you translated the value of yes to はい in your system?
    – Jaypan
    Jul 16 at 16:59

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.

  • 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... ?
    – Rog Boy
    Jul 19 at 7:47
  • Do you mean from $conf['locale_custom_strings_ja']? That is automatically done from Drupal.
    – apaderno
    Jul 19 at 7:52
  • 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
    Jul 19 at 7:57
  • would be very thankful i m new and stuck in this task from friday
    – Rog Boy
    Jul 19 at 7:58
  • 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
    Jul 19 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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