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.

5
  • 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
    Commented May 8, 2014 at 11:35
  • check the global $language variable Commented Jul 7, 2014 at 17:40
  • I'll check it again, but I don't think that the variable_realm_key_language is there. Do you?
    – rreiss
    Commented Jul 8, 2014 at 8:49
  • No, but if you're looking to use i18n_variable_get($name, $langcode), $langcode is definitely in $language. Commented Jul 8, 2014 at 11:07
  • But the langcode shows the current langcode and not the i18n current language (when using variables translation)
    – rreiss
    Commented Jul 8, 2014 at 14:40

2 Answers 2

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.

2
  • This was also my solution as well, but it doesn't feel right.. Still wondering what is the best practice.
    – rreiss
    Commented Nov 4, 2014 at 19:45
  • you can call global $language; and use $language->language rather then getting it from url
    – David
    Commented Dec 29, 2016 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 and acknowledge you have read our privacy policy.

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