2

I'm using a settings form for my module and I'm trying to make some of the fields multilingual.

I've declared the multilingual variables with hook_variable_variable_info (and with hook_variable_group_info for my custom group):

function MYMODULE_variable_info() {
  $variable['my_variable_name'] = array(
    'type' => 'string',
    'title' => t('My title'),
    'default' => MY_DEFAULT_VALUE,
    'description' => t('My description.'),
    'localize' => TRUE,
    'group' => 'my_group'
  );

  return $variable;
}

I've added my settings form (one field for example)

function MYMODULE_settings_form() {
  $form = array();

  $form['my_variable_name'] = array(
    '#type' => 'textfield',
    '#title' => t('My title'),
    '#title_display' => 'before',
    '#default_value' => variable_get_value('my_variable_name'),
  );

  return system_settings_form($form); 
}

And as you can see I've used system_settings_form($form) to save my form fields as variables.
I've checked my new variable at 'admin/config/regional/i18n/variable' and the form shows that it's a multilingual form and multilingual field, but when I'm switching to another language the value isn't saved. (Only the English version saves and changes the value for all languages)

Thanks.

  • The problem seems to be that I'm using variable_get_value and not i18n_variable_get, but how do I get the current 'variable_realm_key_language' so I'll know the current language? – rreiss May 8 '14 at 11:35
  • check the global $language variable – Scott Joudry Jul 7 '14 at 17:40
  • I'll check it again, but I don't think that the variable_realm_key_language is there. Do you? – rreiss Jul 8 '14 at 8:49
  • No, but if you're looking to use i18n_variable_get($name, $langcode), $langcode is definitely in $language. – Scott Joudry Jul 8 '14 at 11:07
  • But the langcode shows the current langcode and not the i18n current language (when using variables translation) – rreiss Jul 8 '14 at 14:40
1

Not sure if it's the best solution at all, but one way would be getting the langcode from the url parameters:

$langcode = !empty($_GET['variable_realm_key_language']) ? $_GET['variable_realm_key_language'] : $GLOBALS['language']->language;

And then you can use i18n_variable_get() using the $langcode as the second parameter.

  • This was also my solution as well, but it doesn't feel right.. Still wondering what is the best practice. – rreiss Nov 4 '14 at 19:45
  • you can call global $language; and use $language->language rather then getting it from url – David Dec 29 '16 at 15:10
3

I was in a need to create translatable strings for text entered using Page title module and I used the following code to create variable

function module_variable_info($options) {
    $variables['page_title_type_news_and_event'] = array(
    'type' => 'text',
    'title' => t('Page title CT New Events', array(), $options),
    'default' => '',
    'description' => t('Module : Page title', array(), $options),
  );

  return $variables;
}

Now I can see them in others tab at admin/config/regional/i18n/variable and I enabled the check box there

enter image description here

Then I can translate my string at admin/config/system/variable and at others tab

enter image description here

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.