mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-08 19:21:40 +09:00
Add i18n class and world country list
This commit is contained in:
parent
56302f58b4
commit
5acc339184
3 changed files with 2378 additions and 0 deletions
2259
common/defaults/countries.php
Normal file
2259
common/defaults/countries.php
Normal file
File diff suppressed because it is too large
Load diff
78
common/framework/i18n.php
Normal file
78
common/framework/i18n.php
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework;
|
||||
|
||||
/**
|
||||
* The i18n (internationalization) class.
|
||||
*/
|
||||
class i18n
|
||||
{
|
||||
/**
|
||||
* Constants for sorting.
|
||||
*/
|
||||
const SORT_CODE_2 = 2;
|
||||
const SORT_CODE_3 = 3;
|
||||
const SORT_CODE_NUMERIC = 4;
|
||||
const SORT_CCTLD = 5;
|
||||
const SORT_NAME_ENGLISH = 6;
|
||||
const SORT_NAME_KOREAN = 7;
|
||||
const SORT_NAME_NATIVE = 8;
|
||||
|
||||
/**
|
||||
* Get the list of all countries.
|
||||
*
|
||||
* @param int $sort_by
|
||||
* @return array
|
||||
*/
|
||||
public static function listCountries($sort_by = self::SORT_NAME_ENGLISH)
|
||||
{
|
||||
$countries = (include \RX_BASEDIR . 'common/defaults/countries.php');
|
||||
$result = array();
|
||||
|
||||
foreach ($countries as $country)
|
||||
{
|
||||
$result[$country['iso_3166_1_alpha3']] = (object)$country;
|
||||
}
|
||||
|
||||
switch ($sort_by)
|
||||
{
|
||||
case self::SORT_CODE_2:
|
||||
uasort($result, function($a, $b) {
|
||||
return strcmp($a->iso_3166_1_alpha2, $b->iso_3166_1_alpha2);
|
||||
});
|
||||
break;
|
||||
case self::SORT_CODE_3:
|
||||
uasort($result, function($a, $b) {
|
||||
return strcmp($a->iso_3166_1_alpha3, $b->iso_3166_1_alpha3);
|
||||
});
|
||||
break;
|
||||
case self::SORT_CODE_NUMERIC:
|
||||
uasort($result, function($a, $b) {
|
||||
return strcmp($a->iso_3166_1_numeric, $b->iso_3166_1_numeric);
|
||||
});
|
||||
break;
|
||||
case self::SORT_CCTLD:
|
||||
uasort($result, function($a, $b) {
|
||||
return strcmp($a->cctld, $b->cctld);
|
||||
});
|
||||
break;
|
||||
case self::SORT_NAME_ENGLISH:
|
||||
uasort($result, function($a, $b) {
|
||||
return strcmp($a->name_english, $b->name_english);
|
||||
});
|
||||
break;
|
||||
case self::SORT_NAME_KOREAN:
|
||||
uasort($result, function($a, $b) {
|
||||
return strcmp($a->name_korean, $b->name_korean);
|
||||
});
|
||||
break;
|
||||
case self::SORT_NAME_NATIVE:
|
||||
uasort($result, function($a, $b) {
|
||||
return strcmp($a->name_native, $b->name_native);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
41
tests/unit/framework/i18nTest.php
Normal file
41
tests/unit/framework/i18nTest.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
class i18nTest extends \Codeception\TestCase\Test
|
||||
{
|
||||
public function testListCountries()
|
||||
{
|
||||
$sort_code_2 = Rhymix\Framework\i18n::listCountries(Rhymix\Framework\i18n::SORT_CODE_2);
|
||||
$this->assertEquals('AD', array_first($sort_code_2)->iso_3166_1_alpha2);
|
||||
$this->assertEquals('AND', array_first_key($sort_code_2));
|
||||
|
||||
$sort_code_3 = Rhymix\Framework\i18n::listCountries(Rhymix\Framework\i18n::SORT_CODE_3);
|
||||
$this->assertEquals('ABW', array_first($sort_code_3)->iso_3166_1_alpha3);
|
||||
$this->assertEquals('ABW', array_first_key($sort_code_3));
|
||||
|
||||
$sort_code_n = Rhymix\Framework\i18n::listCountries(Rhymix\Framework\i18n::SORT_CODE_NUMERIC);
|
||||
$this->assertEquals('004', array_first($sort_code_n)->iso_3166_1_numeric);
|
||||
$this->assertEquals('AFG', array_first_key($sort_code_n));
|
||||
|
||||
$sort_cctld = Rhymix\Framework\i18n::listCountries(Rhymix\Framework\i18n::SORT_CCTLD);
|
||||
$this->assertEquals('zw', array_last($sort_cctld)->cctld);
|
||||
$this->assertEquals('ZWE', array_last_key($sort_cctld));
|
||||
|
||||
$sort_english = Rhymix\Framework\i18n::listCountries(Rhymix\Framework\i18n::SORT_NAME_ENGLISH);
|
||||
$this->assertEquals('Afghanistan', array_first($sort_english)->name_english);
|
||||
$this->assertEquals('AFG', array_first_key($sort_english));
|
||||
$this->assertEquals('Åland Islands', array_last($sort_english)->name_english);
|
||||
$this->assertEquals('ALA', array_last_key($sort_english));
|
||||
|
||||
$sort_korean = Rhymix\Framework\i18n::listCountries(Rhymix\Framework\i18n::SORT_NAME_KOREAN);
|
||||
$this->assertEquals('가나', array_first($sort_korean)->name_korean);
|
||||
$this->assertEquals('GHA', array_first_key($sort_korean));
|
||||
$this->assertEquals('홍콩', array_last($sort_korean)->name_korean);
|
||||
$this->assertEquals('HKG', array_last_key($sort_korean));
|
||||
|
||||
$sort_native = Rhymix\Framework\i18n::listCountries(Rhymix\Framework\i18n::SORT_NAME_NATIVE);
|
||||
$this->assertEquals('Amerika Sāmoa', array_first($sort_native)->name_native);
|
||||
$this->assertEquals('대한민국', $sort_native['KOR']->name_korean);
|
||||
$this->assertEquals('United States of America', $sort_native['USA']->name_english);
|
||||
$this->assertEquals('nz', $sort_native['NZL']->cctld);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue