Saturday
3
April
2010

Using countries for localization

Hy interested ones,

I did not find a correct heading for what I want to describe you today.
But there is a new feature which has been implemented by me so let’s see some details:

Most of you are already using Zend_Locale. Give a language and get a locale in return.
You used this for translation, currency, date or other purposes.

Now the idea for this new feature is simple… why should a user not be able to give a country instead of a language to get a locale in return. For example “austria”… when I give “AT” then I could expect that the locale “de_AT” is being used.

And exactly this feature is available as with ZF 1.10.4 or ZF 2.0.

Of course there are much other usecases where this feature could be used.
For example, when your user selects a flag which represents his country. Then you could use only the region/country to get a locale with language in return.

$locale = new Zend_Locale('US');
// returns "en_US"

Or think of currencies. You could just give the country and get the currency for this country.

$currency = new Zend_Currency(array('locale' => 'AT'));
// uses "EUR" which is the currency for the locale "de_AT"

Generally you can use this feature everywhere within the whole framework where you used only languages or locales before.

But of course there is one thing to note:
You have to uppercase countries when you want to use them. The reason is that there are countries which are equal to languages. For example “om”… you could mean the country Oman and expect “ar_OM” as locale, or you could mean the language “Oromo” and expect “or_KE” as locale.

When you find this feature useful, feel free to use it yourself. :-)

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
Saturday
27
March
2010

Zend_Translate goes configuration

Hy interested ones,

This is my third entry for Zend_Translate this week. Maybe I should call it “Zend_Translate“-week. :-)

Ok… so what have I done this time.
I reworked Zend_Translate a little bit. Now it supports Zend_Config and additionally all options can be given as array.

The change seems small but makes coding a little bit easier and more comfortable.
Previously you had to do:

new Zend_Translate('csv', '\path\to\myfile.csv', null, array('delimiter' => ':'));

Now you can do:

new Zend_Translate(
    array(
        'adapter' => 'csv',
        'content' => '\path\to\myfile.csv',
        'delimiter' => ':'
    )
);

By using the array syntax the code is much more readable and seems more logical.
Of course you can also use a Zend_Config object instead of an array.

This syntax is also supported by using addTranslation(), but as you call addTranslation() on the adapter, you can’t change the adapter within this method so it will be ignored in that case.

$translate->addTranslation(
    array(
        'content' => '\path\to\translations\',
        'locale'    => 'de',
        'scan'      => Zend_Translate::LOCALE_DIRECTORY
    )
);

Note that this new feature is available as with ZF 1.10.3.
Of course the old syntax is still supported. So your old code works without that it has to be reworked (when you don’t like to do that).

When you find this feature useful, feel free to use it yourself. :-)

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
Saturday
27
March
2010

Searching for translations

When you want to add translations then you have probably already used the scan option from Zend_Translate.
It allows you to search complete directory structures for translations.

The scan option has two ways how it can be used.
LOCALE_FILENAME and LOCALE_DIRECTORY.

They define how the language will be detected out of the directory and filename.
LOCALE_FILENAME means that the locale/language this translation file will be added to, will be detected by looking within the filename. And LOCALE_DIRECTORY does the same for the directory where this file has been detected.
Some example:

\dir\en\translation.xxx  <=  locale as directory "en"
\dir_en\translation.xxx  <=  locale as directory "en"
\dir\en_translation.xxx  <=  locale as filename "en"
\dir\translation.en.xxx   <= locale as filename "en"

Simple as is and for most of you nothing new.

But sometimes you may need to exclude some of the files and directories within the structure you are searching.
Therefor you can use the ignore option.

As with 1.10.3 this option accepts 3 syntax to be used.

You can use a single string… per default it is set to “.” which means that all files and directories beginning with “.” are ignored. For example all SVN directories begin with “.” and are per default ignored. All files within this path will not be added.

array('ignore' => '.');  <=  default for ignoring all SVN directories
array('ignore' => 'test');  <=  ignores all directories and files beginning with 'test'

The second line would ignore “\dir\test\en.mo” or “\dir\testme.mo” but it would accept “\dir\mytest.mo” as the later doe s not begin with “test”. You may want to change this option. But be aware that, when you overwrite the “.”, all SVN directories will be searched. So don’t do this when you are working on or with SVN.

As second syntax you can use an array. This allows you to search for several syntax. But also using an array means, like using strings, that the directory or file has to begin with this pattern.

array('ignore' => array('.', 'test');  <=  ignores SVN directories AND also those beginning with 'test'

Still there could be a problem… the directory or filename could have a syntax where it does not begin with a string. Here you have to use the third syntax. It allows you to use regular expressions to search for files and directories which have to be ignored.

array('ignore' => array('regex' => '/test/u', 'regex_2' => '/delete$/u');

The above example would ignore all files and directories which contain test anywhere within their name, and also all files and directories which end with delete.

This should give you enough flexibility to add only those translations which you need and ignore all others.
I hope you find this feature useful. More to come soon…

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top