Question

I have a list of countries (i.e. 'de' => 'Deutschland', 'at' => 'Austria').
What I now need is to get the locale string ('de_DE' or 'de_AT') from the countrycode.

I need this for the validation of the postcode by the selected country. To achieve this, the postcode validator needs a locale string with region ('de_AT' or 'en_US'). 'de' or 'en' does'nt work.

Is there any simple way to do that?

Was it helpful?

Solution

An other solution:

$contry = 'at';
$locale = Zend_Locale::getLocaleToTerritory($contry);
$validator = new Zend_Validate_PostCode($locale);

Zend_Locale::getLocaleToTerritory() retrieve the correct locale by default. (like 'US' give 'en_US')

OTHER TIPS

Try this:

$locale = new Zend_Locale('de');
var_dump($locale->toString());

Here's the documentation: http://framework.zend.com/manual/1.0/en/zend.locale.functions.html

Suppose $contry is 'de' or 'at'.
I guess the language is German ('de')
So you can get the locale like this:

$locale = new Zend_Locale('de_' . strtoupper($contry));

You can use a validator like this:

$validator = new Zend_Validate_PostCode('de_' . strtoupper($contry));

or

$validator = new Zend_Validate_PostCode($locale);

Edit1
with your comment:

If you have just the region, you can't get the locale.
For example, if region is 'et' you can't know if locale is 'aa_ET' or 'am_ET'.
So I think the better soluton is to modify your list to get the good value in the POST.
For example, put 'en_ZW' instead of 'zw'


Edit2

After verification, if you just want the validator Zend_Validate_PostCode, there is a way.

Indeed, each local xx_XX means the language and region and for all languages ​​in the same region, there is the same format postcode.
Here is the code that you will recover the validator:

$contry = 'at';
$list_locale =  Zend_Locale::getLocaleList();
$contry_caps = strtoupper($contry);

foreach($list_locale as $locale_str => $bool){
    $locale_obj = new Zend_Locale($locale_str);
    if ($contry_caps == $locale_obj->getRegion()){
        $locale = $locale_str;
        break;
    }            
}

$validator = new Zend_Validate_PostCode($locale);

However, be aware that somes regions does not have postal code defined in Zend (the version I used is 1.12.6).
Here are the region that does not have a validator for the ZIP code:

001
AE
EH
LY
PS
QA
YE
BT
150
AG
BS
BZ
DM
FJ
GD
GI
GM
GY
JM
KI
KN
KY
LC
MW
SB
SL
SS
TT
UM
VC
VG
WS
419
BO
CO
CU
EA
IC
PA
PE
SV
IR
BF
BJ
BL
CI
GA
MF
KP
MM
AW
CW
SR
SX
ST
TL
AL
VN
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top