twitter_cldr 1.7.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/History.txt +6 -0
  2. data/README.md +61 -5
  3. data/Rakefile +64 -60
  4. data/js/lib/twitter_cldr_js.rb +0 -2
  5. data/lib/twitter_cldr/core_ext.rb +12 -12
  6. data/lib/twitter_cldr/formatters/calendars/timespan_formatter.rb +13 -11
  7. data/lib/twitter_cldr/localized/localized_array.rb +33 -0
  8. data/lib/twitter_cldr/localized/localized_date.rb +23 -0
  9. data/lib/twitter_cldr/localized/localized_datetime.rb +63 -0
  10. data/lib/twitter_cldr/localized/localized_number.rb +50 -0
  11. data/lib/twitter_cldr/localized/localized_object.rb +38 -0
  12. data/lib/twitter_cldr/localized/localized_string.rb +41 -0
  13. data/lib/twitter_cldr/localized/localized_symbol.rb +20 -0
  14. data/lib/twitter_cldr/localized/localized_time.rb +23 -0
  15. data/lib/twitter_cldr/localized/localized_timespan.rb +26 -0
  16. data/lib/twitter_cldr/localized.rb +18 -0
  17. data/lib/twitter_cldr/normalization.rb +23 -0
  18. data/lib/twitter_cldr/resources/{tries_dumper.rb → collation_tries_dumper.rb} +1 -1
  19. data/lib/twitter_cldr/resources/composition_exclusions_importer.rb +1 -1
  20. data/lib/twitter_cldr/resources/language_codes_importer.rb +232 -0
  21. data/lib/twitter_cldr/resources/locales_resources_importer.rb +1 -1
  22. data/lib/twitter_cldr/resources/phone_codes_importer.rb +1 -1
  23. data/lib/twitter_cldr/resources/postal_codes_importer.rb +1 -1
  24. data/lib/twitter_cldr/resources/tailoring_importer.rb +12 -3
  25. data/lib/twitter_cldr/resources/unicode_data_importer.rb +3 -1
  26. data/lib/twitter_cldr/resources.rb +2 -1
  27. data/lib/twitter_cldr/shared/calendar.rb +2 -6
  28. data/lib/twitter_cldr/shared/language_codes.rb +75 -0
  29. data/lib/twitter_cldr/shared/languages.rb +4 -11
  30. data/lib/twitter_cldr/shared.rb +8 -7
  31. data/lib/twitter_cldr/tokenizers/base.rb +2 -8
  32. data/lib/twitter_cldr/utils.rb +8 -0
  33. data/lib/twitter_cldr/version.rb +1 -1
  34. data/lib/twitter_cldr.rb +5 -4
  35. data/resources/custom/locales/cs/units.yml +3 -3
  36. data/resources/custom/locales/pl/units.yml +4 -4
  37. data/resources/custom/locales/pt/units.yml +2 -2
  38. data/resources/shared/language_codes_table.dump +0 -0
  39. data/spec/core_ext_spec.rb +19 -0
  40. data/spec/{core_ext/array_spec.rb → localized/localized_array_spec.rb} +1 -1
  41. data/spec/{core_ext/calendars/date_spec.rb → localized/localized_date_spec.rb} +24 -44
  42. data/spec/localized/localized_datetime_spec.rb +81 -0
  43. data/spec/{core_ext/numbers → localized}/localized_number_spec.rb +34 -1
  44. data/spec/localized/localized_object_spec.rb +89 -0
  45. data/spec/{core_ext/string_spec.rb → localized/localized_string_spec.rb} +16 -33
  46. data/spec/{core_ext/symbol_spec.rb → localized/localized_symbol_spec.rb} +3 -1
  47. data/spec/localized/localized_time_spec.rb +70 -0
  48. data/spec/normalization_spec.rb +42 -0
  49. data/spec/readme_spec.rb +51 -5
  50. data/spec/shared/language_codes_spec.rb +161 -0
  51. data/spec/shared/phone_codes_spec.rb +2 -2
  52. data/spec/shared/postal_codes_spec.rb +2 -2
  53. data/spec/spec_helper.rb +2 -0
  54. data/spec/tokenizers/base_spec.rb +15 -6
  55. data/spec/utils_spec.rb +18 -2
  56. data/twitter_cldr.gemspec +2 -1
  57. metadata +28 -44
  58. data/lib/twitter_cldr/core_ext/array.rb +0 -35
  59. data/lib/twitter_cldr/core_ext/calendars/date.rb +0 -25
  60. data/lib/twitter_cldr/core_ext/calendars/datetime.rb +0 -65
  61. data/lib/twitter_cldr/core_ext/calendars/time.rb +0 -25
  62. data/lib/twitter_cldr/core_ext/calendars/timespan.rb +0 -24
  63. data/lib/twitter_cldr/core_ext/localized_object.rb +0 -25
  64. data/lib/twitter_cldr/core_ext/numbers/bignum.rb +0 -8
  65. data/lib/twitter_cldr/core_ext/numbers/fixnum.rb +0 -8
  66. data/lib/twitter_cldr/core_ext/numbers/float.rb +0 -8
  67. data/lib/twitter_cldr/core_ext/numbers/localized_number.rb +0 -54
  68. data/lib/twitter_cldr/core_ext/string.rb +0 -51
  69. data/lib/twitter_cldr/core_ext/symbol.rb +0 -22
  70. data/spec/core_ext/calendars/datetime_spec.rb +0 -90
  71. data/spec/core_ext/calendars/time_spec.rb +0 -90
  72. data/spec/core_ext/calendars_spec.rb +0 -34
  73. data/spec/core_ext/numbers/bignum_spec.rb +0 -25
  74. data/spec/core_ext/numbers/fixnum_spec.rb +0 -25
  75. data/spec/core_ext/numbers/float_spec.rb +0 -25
  76. data/spec/core_ext/numbers_spec.rb +0 -39
@@ -1,35 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- class Array
7
- def localize(locale = TwitterCldr.get_locale)
8
- TwitterCldr::LocalizedArray.new(self, locale)
9
- end
10
- end
11
-
12
- module TwitterCldr
13
- class LocalizedArray < LocalizedObject
14
- def code_points_to_string
15
- TwitterCldr::Utils::CodePoints.to_string(base_obj)
16
- end
17
-
18
- def sort
19
- TwitterCldr::Collation::Collator.new(locale).sort(base_obj).localize
20
- end
21
-
22
- def sort!
23
- TwitterCldr::Collation::Collator.new(locale).sort!(base_obj)
24
- self
25
- end
26
-
27
- def formatter_const
28
- nil
29
- end
30
-
31
- def to_a
32
- @base_obj.dup
33
- end
34
- end
35
- end
@@ -1,25 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- class Date
7
- def localize(locale = TwitterCldr.get_locale, options = {})
8
- TwitterCldr::LocalizedDate.new(self, locale, options)
9
- end
10
- end
11
-
12
- module TwitterCldr
13
- class LocalizedDate < LocalizedDateTime
14
- def to_datetime(time)
15
- time_obj = time.is_a?(LocalizedTime) ? time.base_obj : time
16
- LocalizedDateTime.new(DateTime.parse("#{@base_obj.strftime("%Y-%m-%d")}T#{time_obj.strftime("%H:%M:%S%z")}"), @locale, :calendar_type => @calendar_type)
17
- end
18
-
19
- protected
20
-
21
- def formatter_const
22
- TwitterCldr::Formatters::DateFormatter
23
- end
24
- end
25
- end
@@ -1,65 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- class DateTime
7
- def localize(locale = TwitterCldr.get_locale, options = {})
8
- TwitterCldr::LocalizedDateTime.new(self, locale, options)
9
- end
10
- end
11
-
12
- module TwitterCldr
13
- class LocalizedDateTime < LocalizedObject
14
- attr_reader :calendar_type
15
-
16
- def initialize(obj, locale, options = {})
17
- super
18
- @calendar_type = options[:calendar_type] || TwitterCldr::DEFAULT_CALENDAR_TYPE
19
- end
20
-
21
- TwitterCldr::Tokenizers::DateTimeTokenizer::VALID_TYPES.each do |format_type|
22
- define_method "to_#{format_type}_s" do
23
- @formatter.format(@base_obj, :type => format_type.to_sym)
24
- end
25
- end
26
-
27
- def to_timespan(options = {})
28
- base_time = options[:base_time] || Time.now
29
- seconds = (self.to_time.base_obj.to_i - base_time.to_i).abs
30
- TwitterCldr::LocalizedTimespan.new(seconds, options.merge(:locale => @locale, :direction => :none))
31
- end
32
-
33
- def ago(options = {})
34
- base_time = options[:base_time] || Time.now
35
- seconds = self.to_time.base_obj.to_i - base_time.to_i
36
- raise ArgumentError.new('Start date is after end date. Consider using "until" function.') if seconds > 0
37
- TwitterCldr::LocalizedTimespan.new(seconds, options.merge(:locale => @locale))
38
- end
39
-
40
- def until(options = {})
41
- base_time = options[:base_time] || Time.now
42
- seconds = self.to_time.base_obj.to_i - base_time.to_i
43
- raise ArgumentError.new('End date is before start date. Consider using "ago" function.') if seconds < 0
44
- TwitterCldr::LocalizedTimespan.new(seconds, options.merge(:locale => @locale))
45
- end
46
-
47
- def to_s
48
- to_default_s
49
- end
50
-
51
- def to_date
52
- LocalizedDate.new(Date.parse(@base_obj.strftime("%Y-%m-%dT%H:%M:%S%z")), @locale, :calendar_type => @calendar_type)
53
- end
54
-
55
- def to_time
56
- LocalizedTime.new(Time.parse(@base_obj.strftime("%Y-%m-%dT%H:%M:%S%z")), @locale, :calendar_type => @calendar_type)
57
- end
58
-
59
- protected
60
-
61
- def formatter_const
62
- TwitterCldr::Formatters::DateTimeFormatter
63
- end
64
- end
65
- end
@@ -1,25 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- class Time
7
- def localize(locale = TwitterCldr.get_locale, options = {})
8
- TwitterCldr::LocalizedTime.new(self, locale, options)
9
- end
10
- end
11
-
12
- module TwitterCldr
13
- class LocalizedTime < LocalizedDateTime
14
- def to_datetime(date)
15
- date_obj = date.is_a?(LocalizedDate) ? date.base_obj : date
16
- LocalizedDateTime.new(DateTime.parse("#{date_obj.strftime("%Y-%m-%d")}T#{@base_obj.strftime("%H:%M:%S%z")}"), @locale, :calendar_type => @calendar_type)
17
- end
18
-
19
- protected
20
-
21
- def formatter_const
22
- TwitterCldr::Formatters::TimeFormatter
23
- end
24
- end
25
- end
@@ -1,24 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- module TwitterCldr
7
- class LocalizedTimespan < LocalizedObject
8
-
9
- def initialize(seconds, options = {})
10
- super(seconds, options[:locale], options)
11
- @formatter = TwitterCldr::Formatters::TimespanFormatter.new(options)
12
- end
13
-
14
- def to_s(options = {})
15
- @formatter.format(@base_obj, options)
16
- end
17
-
18
- protected
19
-
20
- def formatter_const
21
- TwitterCldr::Formatters::TimespanFormatter
22
- end
23
- end
24
- end
@@ -1,25 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- module TwitterCldr
7
- class LocalizedObject
8
- attr_reader :locale, :base_obj, :formatter
9
-
10
- def initialize(obj, locale, options = {})
11
- @base_obj = obj
12
- @locale = TwitterCldr.convert_locale(locale)
13
- @locale = TwitterCldr::DEFAULT_LOCALE unless TwitterCldr.supported_locale?(@locale)
14
-
15
- options = options.dup
16
- options[:locale] = @locale
17
-
18
- @formatter = formatter_const.new(options) if formatter_const
19
- end
20
-
21
- def formatter_const
22
- raise NotImplementedError
23
- end
24
- end
25
- end
@@ -1,8 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- class Bignum
7
- include TwitterCldr::LocalizedNumberMixin
8
- end
@@ -1,8 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- class Fixnum
7
- include TwitterCldr::LocalizedNumberMixin
8
- end
@@ -1,8 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- class Float
7
- include TwitterCldr::LocalizedNumberMixin
8
- end
@@ -1,54 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- module TwitterCldr
7
-
8
- module LocalizedNumberMixin
9
- def localize(locale = TwitterCldr.get_locale)
10
- TwitterCldr::LocalizedNumber.new(self, locale)
11
- end
12
- end
13
-
14
- class LocalizedNumber < LocalizedObject
15
- TYPES = [:decimal, :currency, :percent]
16
- DEFAULT_TYPE = :decimal
17
-
18
- attr_reader :type
19
-
20
- def initialize(obj, locale, options = {})
21
- @options = options.dup
22
-
23
- @type = @options.delete(:type) || DEFAULT_TYPE
24
- raise ArgumentError.new("type #{@type} is not supported") unless @type && TYPES.include?(@type.to_sym)
25
-
26
- super(obj, locale, @options)
27
- end
28
-
29
- TYPES.each do |type|
30
- define_method "to_#{type}" do
31
- to_type(type)
32
- end
33
- end
34
-
35
- def to_s(options = {})
36
- @formatter.format(@base_obj, options)
37
- end
38
-
39
- def plural_rule
40
- TwitterCldr::Formatters::Plurals::Rules.rule_for(@base_obj, @locale)
41
- end
42
-
43
- protected
44
-
45
- def formatter_const
46
- TwitterCldr::Formatters.const_get("#{@type.to_s.capitalize}Formatter")
47
- end
48
-
49
- def to_type(target_type)
50
- self.class.new(@base_obj, @locale, @options.merge(:type => target_type))
51
- end
52
- end
53
-
54
- end
@@ -1,51 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- class String
7
- def localize(locale = TwitterCldr.get_locale)
8
- TwitterCldr::LocalizedString.new(self, locale)
9
- end
10
- end
11
-
12
- module TwitterCldr
13
- class LocalizedString < LocalizedObject
14
- VALID_NORMALIZERS = [:NFD, :NFKD, :NFC, :NFKC]
15
-
16
- # Uses wrapped string object as a format specification and returns the result of applying it to +args+ (see
17
- # +TwitterCldr::Utils.interpolate+ method for interpolation syntax).
18
- #
19
- # If +args+ is a Hash than pluralization is performed before interpolation (see +PluralFormatter+ class for
20
- # pluralization specification).
21
- #
22
- def %(args)
23
- pluralized = args.is_a?(Hash) ? @formatter.format(@base_obj, args) : @base_obj
24
- TwitterCldr::Utils.interpolate(pluralized, args)
25
- end
26
-
27
- def formatter_const
28
- TwitterCldr::Formatters::PluralFormatter
29
- end
30
-
31
- def normalize(options = {})
32
- options[:using] ||= :NFD
33
-
34
- if VALID_NORMALIZERS.include?(options[:using])
35
- normalizer_const = TwitterCldr::Normalization.const_get(options[:using])
36
- LocalizedString.new(normalizer_const.normalize(@base_obj), @locale)
37
- else
38
- raise ArgumentError.new("Invalid normalization form specified with :using option. Choices are [#{VALID_NORMALIZERS.map(&:to_s).join(", ")}]")
39
- end
40
- end
41
-
42
- def code_points
43
- TwitterCldr::Utils::CodePoints.from_string(@base_obj)
44
- end
45
-
46
- def to_s
47
- @base_obj.dup
48
- end
49
-
50
- end
51
- end
@@ -1,22 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- class Symbol
7
- def localize(locale = TwitterCldr.get_locale)
8
- TwitterCldr::LocalizedSymbol.new(self, locale)
9
- end
10
- end
11
-
12
- module TwitterCldr
13
- class LocalizedSymbol < LocalizedObject
14
- def as_language_code
15
- TwitterCldr::Shared::Languages.from_code_for_locale(@base_obj, @locale)
16
- end
17
-
18
- def formatter_const
19
- nil
20
- end
21
- end
22
- end
@@ -1,90 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # Copyright 2012 Twitter, Inc
4
- # http://www.apache.org/licenses/LICENSE-2.0
5
-
6
- require 'spec_helper'
7
-
8
- include TwitterCldr
9
-
10
- describe DateTime do
11
- describe "#localize" do
12
- let(:date) { DateTime.now }
13
- it "should localize with the given locale, English by default" do
14
- loc_date = date.localize
15
- loc_date.should be_a(LocalizedDateTime)
16
- loc_date.locale.should == :en
17
- loc_date.calendar_type.should == :gregorian
18
- loc_date.base_obj.should == date
19
-
20
- loc_date = DateTime.now.localize(:it)
21
- loc_date.should be_a(LocalizedDateTime)
22
- loc_date.locale.should == :it
23
- end
24
-
25
- it "should localize with the given calendar" do
26
- loc_date = date.localize(:th, :calendar_type => :buddhist)
27
- loc_date.should be_a(LocalizedDateTime)
28
- loc_date.locale.should == :th
29
- loc_date.calendar_type.should == :buddhist
30
- loc_date.base_obj.should == date
31
- end
32
-
33
- it "should forward calendar_type" do
34
- loc_date = date.localize(:th, :calendar_type => :buddhist)
35
- loc_date.to_date.calendar_type.should == :buddhist
36
- loc_date.to_time.calendar_type.should == :buddhist
37
- end
38
-
39
- it "should default to English if the given locale isn't supported" do
40
- loc_date = date.localize(:xx)
41
- loc_date.locale.should == :en
42
- end
43
- end
44
-
45
- describe "stringify" do
46
- it "should stringify with a default calendar" do
47
- #DateTime.now.localize(:th, :calendar_type => :buddhist).to_full_s # It doesn't support era
48
- DateTime.now.localize(:th).to_long_s
49
- DateTime.now.localize(:th).to_medium_s
50
- DateTime.now.localize(:th).to_short_s
51
- end
52
-
53
- it "should stringify with buddhist calendar" do
54
- # Ensure that buddhist calendar data is present in th locale.
55
- TwitterCldr.get_locale_resource(:th, :calendars)[:th][:calendars][:buddhist].should_not(
56
- be_nil, 'buddhist calendar is missing for :th locale (check resources/locales/th/calendars.yml)'
57
- )
58
-
59
- #DateTime.now.localize(:th, :calendar_type => :buddhist).to_full_s # It doesn't support era
60
- DateTime.now.localize(:th, :calendar_type => :buddhist).to_long_s
61
- DateTime.now.localize(:th, :calendar_type => :buddhist).to_medium_s
62
- DateTime.now.localize(:th, :calendar_type => :buddhist).to_short_s
63
- end
64
- end
65
- end
66
-
67
- describe LocalizedDateTime do
68
- describe "#to_date" do
69
- it "should convert to a date" do
70
- date = DateTime.new(1987, 9, 20, 22, 5).localize.to_date
71
- date.should be_a(LocalizedDate)
72
- date.base_obj.strftime("%Y-%m-%d").should == "1987-09-20"
73
- end
74
- end
75
-
76
- describe "#to_time" do
77
- it "should convert to a time" do
78
- time = DateTime.new(1987, 9, 20, 22, 5).localize.to_time
79
- time.should be_a(LocalizedTime)
80
- time.base_obj.getgm.strftime("%H:%M:%S").should == "22:05:00"
81
- end
82
- end
83
-
84
- describe "#to_timespan" do
85
- it "should return a localized timespan with a direction of :none" do
86
- ts = DateTime.new(1987, 9, 20, 22, 5).localize.to_timespan
87
- ts.formatter.instance_variable_get(:'@direction').should == :none
88
- end
89
- end
90
- end