Skip to content

Commit

Permalink
inherit from defined parent locales [fix #34]
Browse files Browse the repository at this point in the history
  • Loading branch information
patch committed Mar 26, 2014
1 parent 59a013a commit 6e6e55f
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 16 deletions.
13 changes: 7 additions & 6 deletions Build.PL
Expand Up @@ -11,12 +11,13 @@ my $builder = Module::Build->new(
'Software::License' => 0,
},
build_requires => {
'open' => 0,
'English' => 0,
'Pod::Man' => '2.04',
'Test::Exception' => '0.22',
'Test::More' => '0.82',
'Test::Warn' => '0.01',
'open' => 0,
'English' => 0,
'Pod::Man' => '2.04',
'Test::Differences' => 0,
'Test::Exception' => '0.22',
'Test::More' => '0.82',
'Test::Warn' => '0.01',
},
requires => {
'perl' => '5.8.1',
Expand Down
126 changes: 126 additions & 0 deletions lib/CLDR/Number/Data/Base.pm
Expand Up @@ -3086,6 +3086,132 @@ our $DATA = {
)
};

our $PARENT = {
'az-Cyrl' => 'root',
'bs-Cyrl' => 'root',
'en-Dsrt' => 'root',
'ha-Arab' => 'root',
'mn-Mong' => 'root',
'ms-Arab' => 'root',
'pa-Arab' => 'root',
'shi-Latn' => 'root',
'sr-Latn' => 'root',
'uz-Arab' => 'root',
'uz-Cyrl' => 'root',
'vai-Latn' => 'root',
'zh-Hant' => 'root',
'en-AG' => 'en-001',
'en-AI' => 'en-001',
'en-BB' => 'en-001',
'en-BM' => 'en-001',
'en-BS' => 'en-001',
'en-BW' => 'en-001',
'en-BZ' => 'en-001',
'en-CC' => 'en-001',
'en-CK' => 'en-001',
'en-CM' => 'en-001',
'en-CX' => 'en-001',
'en-DM' => 'en-001',
'en-ER' => 'en-001',
'en-FJ' => 'en-001',
'en-FM' => 'en-001',
'en-GB' => 'en-001',
'en-GD' => 'en-001',
'en-GH' => 'en-001',
'en-GM' => 'en-001',
'en-GY' => 'en-001',
'en-JM' => 'en-001',
'en-KE' => 'en-001',
'en-KI' => 'en-001',
'en-KN' => 'en-001',
'en-KY' => 'en-001',
'en-LC' => 'en-001',
'en-LR' => 'en-001',
'en-LS' => 'en-001',
'en-MG' => 'en-001',
'en-MS' => 'en-001',
'en-MU' => 'en-001',
'en-MW' => 'en-001',
'en-NA' => 'en-001',
'en-NF' => 'en-001',
'en-NG' => 'en-001',
'en-NR' => 'en-001',
'en-NU' => 'en-001',
'en-PG' => 'en-001',
'en-PH' => 'en-001',
'en-PN' => 'en-001',
'en-PW' => 'en-001',
'en-RW' => 'en-001',
'en-SB' => 'en-001',
'en-SC' => 'en-001',
'en-SD' => 'en-001',
'en-SL' => 'en-001',
'en-SS' => 'en-001',
'en-SX' => 'en-001',
'en-SZ' => 'en-001',
'en-TC' => 'en-001',
'en-TK' => 'en-001',
'en-TO' => 'en-001',
'en-TT' => 'en-001',
'en-TV' => 'en-001',
'en-TZ' => 'en-001',
'en-UG' => 'en-001',
'en-VC' => 'en-001',
'en-VU' => 'en-001',
'en-WS' => 'en-001',
'en-ZA' => 'en-001',
'en-ZM' => 'en-001',
'en-ZW' => 'en-001',
'en-150' => 'en-GB',
'en-AU' => 'en-GB',
'en-BE' => 'en-GB',
'en-DG' => 'en-GB',
'en-FK' => 'en-GB',
'en-GG' => 'en-GB',
'en-GI' => 'en-GB',
'en-HK' => 'en-GB',
'en-IE' => 'en-GB',
'en-IM' => 'en-GB',
'en-IN' => 'en-GB',
'en-IO' => 'en-GB',
'en-JE' => 'en-GB',
'en-MO' => 'en-GB',
'en-MT' => 'en-GB',
'en-NZ' => 'en-GB',
'en-PK' => 'en-GB',
'en-SG' => 'en-GB',
'en-SH' => 'en-GB',
'en-VG' => 'en-GB',
'es-AR' => 'es-419',
'es-BO' => 'es-419',
'es-CL' => 'es-419',
'es-CO' => 'es-419',
'es-CR' => 'es-419',
'es-CU' => 'es-419',
'es-DO' => 'es-419',
'es-EC' => 'es-419',
'es-GT' => 'es-419',
'es-HN' => 'es-419',
'es-MX' => 'es-419',
'es-NI' => 'es-419',
'es-PA' => 'es-419',
'es-PE' => 'es-419',
'es-PR' => 'es-419',
'es-PY' => 'es-419',
'es-SV' => 'es-419',
'es-US' => 'es-419',
'es-UY' => 'es-419',
'es-VE' => 'es-419',
'pt-AO' => 'pt-PT',
'pt-CV' => 'pt-PT',
'pt-GW' => 'pt-PT',
'pt-MO' => 'pt-PT',
'pt-MZ' => 'pt-PT',
'pt-ST' => 'pt-PT',
'pt-TL' => 'pt-PT',
'zh-Hant-MO' => 'zh-Hant-HK',
};

our $CACHE = {
pattern => {
'#0%' => [ '#0', "$N$P" ],
Expand Down
10 changes: 9 additions & 1 deletion lib/CLDR/Number/Role/Base.pm
Expand Up @@ -222,8 +222,16 @@ sub _build_inheritance {
my $locale = join '-', @$subtags;
next if !exists $CLDR::Number::Data::Base::DATA->{$locale};
push @tree, $locale;

if (my $parent = $CLDR::Number::Data::Base::PARENT->{$locale}) {
push @tree, @{_build_inheritance(_split_locale($parent))};
last;
}
}

if (!@tree || $tree[-1] ne 'root') {
push @tree, 'root';
}
push @tree, 'root';

return \@tree;
}
Expand Down
49 changes: 48 additions & 1 deletion t/inheritance.t
Expand Up @@ -2,7 +2,8 @@ use utf8;
use strict;
use warnings;
use open qw( :encoding(UTF-8) :std );
use Test::More tests => 7;
use Test::More tests => 15;
use Test::Differences;
use CLDR::Number;

my $cldr = CLDR::Number->new;
Expand All @@ -17,6 +18,10 @@ $cldr->locale('lu');
is $cldr->decimal_sign, ',', 'decimal directly from lu';
is $cldr->minus_sign, '-', 'minus inherited from root';

$cldr->locale('es-US');
is $cldr->decimal_sign, '.', 'decimal inherited from es-419, not es';
is $cldr->group_sign, ',', 'group inherited from es-419, not es';

my $curf = $cldr->currency_formatter(locale => 'sv-FI');
is $curf->decimal_sign, ':', 'currency decimal inherited from sv';

Expand All @@ -25,3 +30,45 @@ $curf->currency_code('AUD');
is $curf->currency_sign, '$', 'currency sign directly from en-AU';
$curf->currency_code('JPY');
is $curf->currency_sign, '¥', 'currency sign inherited from en';

$cldr->locale('shi-Tfng-MA');
eq_or_diff(
$cldr->_locale_inheritance,
[qw( shi-Tfng-MA shi-Tfng shi root )],
'locale inheritance'
);

$cldr->locale('root');
eq_or_diff(
$cldr->_locale_inheritance,
[qw( root )],
'root has a single level of inheritance'
);

$cldr->locale('es-MX');
eq_or_diff(
$cldr->_locale_inheritance,
[qw( es-MX es-419 es root )],
'locale inheritance with added step'
);

$cldr->locale('az-Cyrl');
eq_or_diff(
$cldr->_locale_inheritance,
[qw( az-Cyrl root )],
'locale inheritance with removed step'
);

$cldr->locale('zh-Hant-MO');
eq_or_diff(
$cldr->_locale_inheritance,
[qw( zh-Hant-MO zh-Hant-HK zh-Hant root )],
'locale inheritance with added and removed steps'
);

$cldr->locale('en-US-u-va-posix');
eq_or_diff(
$cldr->_locale_inheritance,
[qw( en-US-u-va-posix en-US en root )],
'locale inheritance with Unicode extension'
);
9 changes: 1 addition & 8 deletions t/locales.t
Expand Up @@ -2,7 +2,7 @@ use utf8;
use strict;
use warnings;
use open qw( :encoding(UTF-8) :std );
use Test::More tests => 26;
use Test::More tests => 24;
use Test::Warn;
use CLDR::Number;

Expand Down Expand Up @@ -83,10 +83,3 @@ is $cldr->locale, 'en-US', 'locale is language-country when unavailable script';

$cldr->locale('es-419');
is $cldr->locale, 'es-419', 'numeric regions are supported';

# inheritance
$cldr->locale('zh-Hant-HK');
is_deeply $cldr->_locale_inheritance, [qw( zh-Hant-HK zh-Hant zh root )], 'locale inheritance';

$cldr->locale('en-US-u-va-posix');
is_deeply $cldr->_locale_inheritance, [qw( en-US-u-va-posix en-US en root )], 'locale inheritance';

0 comments on commit 6e6e55f

Please sign in to comment.