2

I'm developing a simple module with and admin page, where admin can fill some textfields with custom strings that will generates a piece of html to be used later.

So for example I have fields like this in admin page:

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

  $form['MYMODULE_link'] = array(
    '#type' => 'textfield',
    '#title' => t('Conditions link path'),
    '#default_value' => variable_get('MYMODULE_link', 'conditions'),
  );
  $form['MYMODULE_link_text'] = array(
    '#type' => 'textfield',
    '#title' => t('Conditions link text'),
    '#default_value' => variable_get('MYMODULE_link_text', 'conditions'),
  );
  $form['MYMODULE_link_target'] = array(
    '#type' => 'textfield',
    '#title' => t('Conditions link target'),
    '#default_value' => variable_get('MYMODULE_link_target', '_blank'),
  );
  $form['MYMODULE_prefix'] = array(
    '#type' => 'textfield',
    '#title' => t('Conditions link prefix text'),
    '#default_value' => variable_get('MYMODULE_prefix', 'I agree to the '),
  );
  $form['MYMODULE_suffix'] = array(
    '#type' => 'textfield',
    '#title' => t('Conditions link suffix text'),
    '#default_value' => variable_get('MYMODULE_suffix', '.'),
  );

  return system_settings_form($form);
}

Then the above values are managed after by my module to compose an output like:

// Create label
$prefix = variable_get('MYMODULE_prefix', 'I agree to the ');
$suffix = variable_get('MYMODULE_suffix', '.');
$path = variable_get('MYMODULE_link', 'conditions');
$text = variable_get('MYMODULE_link_text', 'conditions');
$target = variable_get('MYMODULE_link_target', '_blank');

$label = $prefix . l($text, $path, array('attributes' => array('target' => $target))) . $suffix;

Now I need that all these values are available for string translation, so the first think I thought was to pass all of them through t(), for example:

$label = t($prefix) . l(t($text), $path, array('attributes' => array('target' => $target))) . t($suffix);

but I read here https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/t/7 that I should never pass variables directly to t(), so for e.g t($prefix) is a bad stuff.

So, what is the correct Drupal way to accomplish this?

0

1 Answer 1

2

You should use the Variable translation module, part of Internationalization.

  1. Enable the Variable translation module included with the Internationalization package
  2. Go to Administration > Configuration > Regional and language > Multilingual settings
  3. Click on the Variables tab
  4. Select the variables that will be multilingual
  5. Click Save configuration button

Then, you'll be able to select language for which you are configuring your variable:

Screenshot from community documentation

Advice taken from this community documentation.

3
  • Thank you for your answer, but In this case I'll have to introduce a non-core module dependency: I hope there is a core-based solution to this simple "every-day" (I think) necessity
    – MXT
    Sep 23, 2014 at 12:37
  • @MXT Nope. Built in support is limited. Internationalization module was created exactly to fill up the gaps. You can either use i18n as it is, or try to duplicate it, but I see no point in using second option. Someone wrote it already. See NIH philosophy - are you sure that's not what you are doing now?
    – Mołot
    Sep 23, 2014 at 13:43
  • Thank you Molot for your explanation, the issue is more clearer now. I think that in my specific case, where these fields are not public but populated only in protected admin pages by administrators (= trusted people), security risks are minimal so I could however try the use of t($variable). On the other hand the t() API page just suggests "You should never use t() to translate variables", not that it is absolutely forbidden... I don't know, I'd like to get more opinions on this
    – MXT
    Sep 23, 2014 at 21:48

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