I am encountering what seems to be a basic problem. I have a Drupal website translated in 7 languages with a lot of content. I used i18n module and I am happy with it, except for one thing. In template.php and other *.tpl.php I want to put some short sentences. How can I translate them?? I tried to use t('mySentence') function but it doesn't work in these files (no problem in a *.module file). I also tried to use i18n.String experimental module, without success.

Does anybody can give me some help?

Thank you in advance ...

Comments

adrienf’s picture

Version: 5.x-1.0 » master

No idea?

jordanlewin’s picture

I've run into the same issue and have found no success as of yet. It seems this might be a more base issue with the way locale.module processes the t() function. Any one else have any ideas how to get around this? I'm still looking around for a solution to get the Manage Strings functionality to recognize strings in *.tpl.php and template.php files (and even in page or block content, for that matter).

Roberto Gerola’s picture

If you don't need to pass additional arguments, you can use locale function instead :
locale('my string').

Otherwise you can add this modified function in your template.php :

//Function to translate strings in theme files
function tr($string, $args = 0, $return = FALSE) {
  global $locale;
    if (function_exists('locale') && $locale != 'en') {
      $string = locale($string);
  }
  if (!$args) {
    if($return) return $string;
    print $string;
  }
  else {
   // Transform arguments before inserting them
   foreach ($args as $key => $value) {
    switch ($key[0]) {
      // Escaped only
      case '@':
        $args[$key] = check_plain($value);
        break;
      // Escaped and placeholder
      case '%':
      default:
        $args[$key] = theme('placeholder', $value);
        break;
      // Pass-through
      case '!':
    }
  }
  $string = strtr($string, $args);
  if($return) return $string;
  print $string;
}

And use it in this way :
tr('my string');

jose reyero’s picture

Status: Active » Closed (fixed)

Yeah, you can use t() function all around.

Whatever this is plain localization, nothing to do with i18n.

NeilChen’s picture

My drupal version is 6.20
I test it can't working.
I changed:

function tr($string, $args = 0, $return = FALSE) {
  $locale = language_default();
  if (function_exists('locale') && $locale->language != 'en') {
    $string = locale($string, $locale->language);
  }
  if (!$args) {
    return $string;
  } else {
    // Transform arguments before inserting them
    foreach ($args as $key => $value) {
      switch ($key[0]) {
        // Escaped only
        case '@':
          $args[$key] = check_plain($value);
          break;
        // Escaped and placeholder
        case '%':
        default:
          $args[$key] = theme('placeholder', $value);
          break;
        // Pass-through
        case '!':
      }
    }
    $string = strtr($string, $args);
    return $string;
  }
}
NeilChen’s picture

My drupal version is 6.20
I test it can't working.
I changed:

function tr($string, $args = 0, $return = FALSE) {
  $locale = language_default();
  if (function_exists('locale') && $locale->language != 'en') {
    $string = locale($string, $locale->language);
  }
  if (!$args) {
    return $string;
  } else {
    // Transform arguments before inserting them
    foreach ($args as $key => $value) {
      switch ($key[0]) {
        // Escaped only
        case '@':
          $args[$key] = check_plain($value);
          break;
        // Escaped and placeholder
        case '%':
        default:
          $args[$key] = theme('placeholder', $value);
          break;
        // Pass-through
        case '!':
      }
    }
    $string = strtr($string, $args);
    return $string;
  }
}