I am writing a locale simpletest (actually it's written). The test adds a custom language and then translates its name. It works if the default language is not English. If it is English then the string search does not find the name. I added t($name) to my code, no avail. Not even t($name, array(), $langcode) ($langcode being the custom language code) worked but calling t($name, array(), '-') (nonexistent language) caused the search to succeed. I think this is not right. I have content module switched on, dunno whether it matters. Attached is the simpletest which I have not filed into the simpletest queue because I would like to see this resolved first. Line 45 is the interesting part. The test is well commented so even if you do not know simpletest, it should be easy to understand.

CommentFileSizeAuthor
#9 php_is_weird.patch560 byteschx
#7 locale_reset.patch694 byteschx
localeModuleTest.test5.02 KBchx

Comments

theborg’s picture

I cannot reproduce the error inside a site, these are the steps I followed:

a) Enable 'locale' and 'content translation' modules.
b) Import 'French language' and enable it.
c) Create a custom language: name=>prova, prefix=>pp, code=>pp.
d) Create translation of string 'Log out' to invented lang.
e) Create some content and call: t('Log out', Array(), 'pp'). It works ok.

About t() returning the string not found I think this is because of locale function:

 return ($locale_t[$langcode][$string] === TRUE ? $string : $locale_t[$langcode][$string]);

edit: Confirmed, t() will not translate $language->name to $language->native name.

chx’s picture

Hrm, I have not expected that. While $language->name does not show up in the search interface if the default lang is en it does if the def lang is not en.

gábor hojtsy’s picture

Priority: Critical » Normal
Status: Active » Closed (works as designed)

This is a simple performance feature. If the site language is English, *nothing* is passed through from t() to locale() for translation. This is how it is always been: http://api.drupal.org/api/function/t/6

...
elseif (function_exists('locale') && $langcode != 'en') {
    $string = locale($string, $langcode);
}
...

In Drupal, everything on the built-in interface is assumed to be in English by default, so if you see the site in English, there is no need to bother the database with translating your string.

chx’s picture

Status: Closed (works as designed) » Active

Not even t($name, array(), $langcode) ($langcode being the custom language code) worked but calling t($name, array(), '-') (nonexistent language) caused the search to succeed.

gábor hojtsy’s picture

Well, I have this testing code. I run it on a site with English (as default) and German also set up:

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Drop all pre-existing data.
db_query('DELETE FROM {locales_target}');
db_query('DELETE FROM {locales_source}');

// Show what languages we have.
$default = language_default();
$result = db_query('SELECT name, language FROM {languages}');
while ($row = db_fetch_object($result)) {
  echo $row->name . ($row->language == $default->language ? '-Default' : '') .'<br />';
}

// Should not enter this into the DB, if we use English.
t('test-foo');
// Should enter this to the DB.
t('test-bar', array(), 'de');
// Enters into the DB (although non-existent language).
t('test-baz', array(), '-');

// Show what strings we have.
$result = db_query('SELECT source FROM {locales_source}');
while ($row = db_fetch_object($result)) {
  echo $row->source .'<br />';
}

All runs fine, test-foo does not end up there, test-bar and test-baz does. I cannot spot an error here.

chx’s picture

Status: Active » Closed (works as designed)
chx’s picture

Status: Closed (works as designed) » Reviewed & tested by the community
StatusFileSize
new694 bytes
gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

I have added some additional cleanup, and committed this one. The background info is that we discussed this test (and my testing code above) with chx on Skype, and come down to the realization that Drupal works fine, but simpletest does not reload the Drupal instance after the form submission, so the old locale data is used in locale(). This reset parameter allows simpletest to behave, pass with the locale test and aligns locale() with other in-memory cache employing functions.

chx’s picture

Status: Fixed » Reviewed & tested by the community
StatusFileSize
new560 bytes

If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable.

http://php.net/unset

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Committed.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.