0

How can I translate the fields for my custom database table which uses the following schema?

  • member_id: int
  • name: varchar(60)
  • i_prefer: varchar(50)

I need to translate the string that is displayed for the 'i_prefer' field. I know how I should represent this in template page (<?php print t($query->i_prefer); ?>), but the translated string will not appear until I provide the translation. I checked in 'Regional and language > Translate interface'; I can not see my text.

How can I do this?

1

The first argument of t() is a literal string, not a dynamic string. If you use a dynamic string, that will be translated only if it matched a literal string another module passes to t().

For example, if I use the following code, the string will be shown translated (when a translation for the currently enabled language exists), as 'E-mail address' is used as argument of t() from user_account_form().

  $label = 'E-mail address';

  $form['mail'] = array(
    '#type' => 'textfield', 
    '#title' => t($label), 
    '#maxlength' => EMAIL_MAX_LENGTH, 
    '#required' => TRUE, 
    '#default_value' => '',
  );

If the values contained in that database field are limited to specific values, then you can add the following code to the module you are using.

function mymodule_translated_strings() {
  $strings = array(
    t('first value used for the database field'),
    t('second value used for the database field'),
    t('third value used for the database field'),
  );
}

The function doesn't need to be called; it is required only to have a call to t() for each literal strings that contain one possible value of that database field. Clearly, this is convenient if the database field can assume values which are restricted to a small set of values. If the values assigned to the database field are input from users, then this approach is not possible, since the users can (theoretically) enter any value.

For the other cases, the Internationalization module could be helpful. As described in Translating user-defined strings (module developers), i18nstrings() is the function that should be used in this case.

  • in Simple, I have supplied several string in my templates using print t(); function. How would I translate them? I can not see those text in 'translate interface' section of Drupal cms (D7). How I will give alternate translation for them? – Muneer Feb 10 '13 at 13:06
  • It seems Until I execute alternate version of the page in browser which has those text, drupal is not fetching them and adding to db. SOlved – Muneer Feb 10 '13 at 13:20

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.