Zend Translate custom language

847 views Asked by At

Depending on the user role, I need to show different texts in my Zend project.

  • For normal users I'm using the "en" language.
  • For new users I want something like "en_new".

However, the language "en_new" always reverts to just "en".

I'm using the locale_directory scan system to automatically detect languages.

2

There are 2 answers

0
bububaba On

The translate adapter calls Zend_Locale::findLocale() internally in addTranslation() (at least in ZF 1.1x). This in turn checks whether the locale is on a whitelist. Yours is not, obviously. I didn't dig too deep into the code, but it's quite probable that the next step is to revert from en_xxx to just en which is what happens in your case.

See the sources:

  • library/Zend/Translate/Adapter.php - addTranslation method
  • library/Zend/Locale.php - findLocale method
0
enricog On

I am currently evaluating something similar, for some users I want some texts to be differently translated. And I also ran into the problem to not be able to create a custom locale value. Tough what I found out in my tests seems to solve/work around the problem.

See also here: Combining multiple translation sources

What I am doing is to just add a custom translation to my default ones.

$translateDef = new Zend_Translate(
   array(
       'adapter'    => 'gettext',
       'content'    => 'locale/default/',
       'locale' => 'auto',
       'scan'       => Zend_Translate::LOCALE_DIRECTORY
   )
);

$translateCust = new Zend_Translate(
   array(
       'adapter'    => 'gettext',
       'content'    => 'locale/custom/',
       'locale' => 'auto',
       'scan'       => Zend_Translate::LOCALE_DIRECTORY
   )
);

$translateDef->addTranslation(array(
       'content'    => $translateCust
    )
);

And the folder structure looks like this:

locale/
       default/
               de
               en
       custom/
               de
               en

So when doing the addTranslation it seems to overvwrites the existing ones, so for your new users, you could add custom folder with the proper translations. For my tests this worked so far, but haven't evaluated it in depth yet.