0

Long story short: I'm looking for a way to provide translations... the "hard" way. So I don't want to click in the backend to apply new translations or translate it on the "Translate Interface" Page.

I really want to provide it hard coded, and when it's changed, it should get changed in the backend as well.

1 Answer 1

0

Since you don't want to use the user interface, the only way to provide a translation of the string used from modules is to set them in the settings.php file.

$conf['locale_custom_strings_it'][''] = array(
  'forum' => 'Tavola di discussione',
  '@count min' => '@count minuti',
);

Replace "it" with language code for which you want to provide the translation. The array index is the string in English, while the array value is the translated string.

Using this method is pretty hard: Each times a new string is translated, the settings.php needs to be edited. There could be also some performance issues, as you are defining a new array row for every new translation; I let you imagine what happens when you have 1000 strings to translate in 5 languages.

When those values are provided, the database is not anymore looked up, as t() contains the following code. (See the first comment.)

  // First, check for an array of customized strings. If present, use the array
  // *instead of* database lookups. This is a high performance way to provide a
  // handful of string replacements. See settings.php for examples.
  // Cache the $custom_strings variable to improve performance.
  if (!isset($custom_strings[$options['langcode']])) {
    $custom_strings[$options['langcode']] = variable_get('locale_custom_strings_' . $options['langcode'], array());
  }
  // Custom strings work for English too, even if locale module is disabled.
  if (isset($custom_strings[$options['langcode']][$options['context']][$string])) {
    $string = $custom_strings[$options['langcode']][$options['context']][$string];
  }
  // Translate with locale module if enabled.
  elseif ($options['langcode'] != 'en' && function_exists('locale')) {
    $string = locale($string, $options['context'], $options['langcode']);
  }
  if (empty($args)) {
    return $string;
  }
  else {
    return format_string($string, $args);
  }
4
  • Thanks for your answer! When I do it this way, would drupal still read translations from the database?
    – lumio
    Commented Dec 30, 2012 at 18:24
  • I have updated my answer to include that information.
    – avpaderno
    Commented Dec 30, 2012 at 19:17
  • I just checked it. Now it works for the english translation, but not for others. I have i18n and locale enabled and tried to translate German. That didn't work. Also this works only with buildt in strings. For strings for other modules like Rules, it does not work.
    – lumio
    Commented Dec 31, 2012 at 8:29
  • It should work for every language, as the code that uses the "locale_custom_strings_" Drupal variables is part of t(). If a module uses t(), then its strings can be replaced in this way.
    – avpaderno
    Commented Dec 31, 2012 at 8:44

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.