twitter_cldr 1.3.0 → 1.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +58 -10
- data/Rakefile +25 -0
- data/lib/twitter_cldr.rb +16 -1
- data/lib/twitter_cldr/core_ext.rb +1 -0
- data/lib/twitter_cldr/core_ext/calendars/datetime.rb +14 -0
- data/lib/twitter_cldr/core_ext/calendars/timespan.rb +26 -0
- data/lib/twitter_cldr/formatters.rb +1 -0
- data/lib/twitter_cldr/formatters/calendars/datetime_formatter.rb +1 -6
- data/lib/twitter_cldr/formatters/calendars/timespan_formatter.rb +67 -0
- data/lib/twitter_cldr/formatters/numbers/number_formatter.rb +1 -1
- data/lib/twitter_cldr/tokenizers.rb +1 -0
- data/lib/twitter_cldr/tokenizers/base.rb +23 -9
- data/lib/twitter_cldr/tokenizers/calendars/date_tokenizer.rb +14 -8
- data/lib/twitter_cldr/tokenizers/calendars/datetime_tokenizer.rb +13 -9
- data/lib/twitter_cldr/tokenizers/calendars/time_tokenizer.rb +13 -7
- data/lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb +82 -0
- data/lib/twitter_cldr/tokenizers/numbers/number_tokenizer.rb +21 -14
- data/lib/twitter_cldr/tokenizers/token.rb +4 -0
- data/lib/twitter_cldr/version.rb +1 -1
- data/resources/locales/ar/units.yml +149 -0
- data/resources/locales/da/units.yml +79 -0
- data/resources/locales/de/units.yml +93 -0
- data/resources/locales/en/units.yml +65 -0
- data/resources/locales/es/units.yml +93 -0
- data/resources/locales/fa/units.yml +86 -0
- data/resources/locales/fi/units.yml +93 -0
- data/resources/locales/fil/units.yml +79 -0
- data/resources/locales/fr/units.yml +93 -0
- data/resources/locales/he/units.yml +79 -0
- data/resources/locales/hi/units.yml +79 -0
- data/resources/locales/hu/units.yml +72 -0
- data/resources/locales/id/units.yml +72 -0
- data/resources/locales/it/units.yml +79 -0
- data/resources/locales/ja/units.yml +72 -0
- data/resources/locales/ko/units.yml +72 -0
- data/resources/locales/ms/units.yml +86 -0
- data/resources/locales/nl/units.yml +79 -0
- data/resources/locales/pl/units.yml +135 -0
- data/resources/locales/pt/units.yml +93 -0
- data/resources/locales/ru/units.yml +135 -0
- data/resources/locales/sv/units.yml +93 -0
- data/resources/locales/th/units.yml +72 -0
- data/resources/locales/tr/units.yml +72 -0
- data/resources/locales/ur/units.yml +86 -0
- data/resources/locales/zh-Hant/units.yml +72 -0
- data/resources/locales/zh/units.yml +72 -0
- data/spec/core_ext/calendars/date_spec.rb +83 -9
- data/spec/core_ext/calendars/datetime_spec.rb +1 -3
- data/spec/core_ext/calendars/time_spec.rb +12 -3
- data/spec/formatters/calendars/timespan_formatter_spec.rb +39 -0
- data/spec/tokenizers/base_spec.rb +10 -10
- data/spec/tokenizers/calendars/timespan_tokenizer_spec.rb +24 -0
- data/spec/tokenizers/numbers/number_tokenizer_spec.rb +1 -1
- data/spec/twitter_cldr_spec.rb +16 -0
- metadata +126 -14
- data/lib/twitter_cldr/tokenizers/key_path.rb +0 -35
- data/spec/tokenizers/key_path_spec.rb +0 -49
data/README.md
CHANGED
@@ -33,14 +33,6 @@ TwitterCldr patches core Ruby objects like Fixnum and Date to make localization
|
|
33
33
|
|
34
34
|
### Numbers
|
35
35
|
|
36
|
-
**Note**: The CLDR is missing complete number data for:
|
37
|
-
|
38
|
-
* hu (Hungarian)
|
39
|
-
* id (Indonesian)
|
40
|
-
* msa (Malay)
|
41
|
-
* no (Norwegian),
|
42
|
-
* zh-tw (Traditional Chinese)
|
43
|
-
|
44
36
|
Fixnum, Bignum, and Float objects are supported. Here are some examples:
|
45
37
|
|
46
38
|
```ruby
|
@@ -328,6 +320,63 @@ No external requirements.
|
|
328
320
|
|
329
321
|
`bundle exec rake` should do the trick. Tests are written in RSpec using RR as the mocking framework.
|
330
322
|
|
323
|
+
## JavaScript Support
|
324
|
+
(Note: These changes have not yet been released as a gem.)
|
325
|
+
|
326
|
+
TwitterCLDR currently supports localization of dates and times in JavaScript. More awesome features are coming soon.
|
327
|
+
|
328
|
+
### Generating the JavaScript
|
329
|
+
|
330
|
+
You can automatically generate the JavaScript versions of TwitterCLDR using this Rubygem. Here's the one-liner:
|
331
|
+
|
332
|
+
`bundle exec rake js:build OUTPUT_DIR=/path/to/desired/output/location`
|
333
|
+
|
334
|
+
If you'd like to customize the generated output further, you'll need to require the `TwitterCldr::Js` namespace. You can choose the locales to export and whether to export a minified version alongside the full version for each locale.
|
335
|
+
|
336
|
+
```ruby
|
337
|
+
require 'twitter_cldr'
|
338
|
+
|
339
|
+
TwitterCldr.require_js # require JavaScript environment
|
340
|
+
TwitterCldr::Js.output_dir = "/path/to/output/location"
|
341
|
+
TwitterCldr::Js.make(:locales => [:de, :sv, :ja, :ar], # generate files for German, Swedish,
|
342
|
+
:minify => true) # Japanese, and Arabic
|
343
|
+
TwitterCldr::Js.install # copy files to output directory
|
344
|
+
```
|
345
|
+
|
346
|
+
### Dates and Times (JS)
|
347
|
+
|
348
|
+
```javascript
|
349
|
+
// include twitter_cldr_es.js for the Spanish DateTimeFormatter
|
350
|
+
var fmt = new TwitterCldr.DateTimeFormatter();
|
351
|
+
|
352
|
+
fmt.format(new Date(), {"type": "full"}); // "lunes, 12 de diciembre de 2011 21:44:57 UTC -0800"
|
353
|
+
fmt.format(new Date(), {"type": "long"}); // "12 de diciembre de 201121:45:42 -08:00"
|
354
|
+
fmt.format(new Date(), {"type": "medium"}); // "12/12/2011 21:46:09"
|
355
|
+
fmt.format(new Date(), {"type": "short"}); // "12/12/11 21:47"
|
356
|
+
|
357
|
+
fmt.format(new Date(), {"format": "date", "type": "full"}); // "lunes, 12 de diciembre de 2011"
|
358
|
+
fmt.format(new Date(), {"format": "date", "type": "long"}); // "12 de diciembre de 2011"
|
359
|
+
fmt.format(new Date(), {"format": "date", "type": "medium"}); // "12/12/2011"
|
360
|
+
fmt.format(new Date(), {"format": "date", "type": "short"}); // "12/12/11"
|
361
|
+
|
362
|
+
fmt.format(new Date(), {"format": "time", "type": "full"}); // "21:44:57 UTC -0800"
|
363
|
+
fmt.format(new Date(), {"format": "time", "type": "long"}); // "21:45:42 -08:00"
|
364
|
+
fmt.format(new Date(), {"format": "time", "type": "medium"}); // "21:46:09"
|
365
|
+
fmt.format(new Date(), {"format": "time", "type": "short"}); // "21:47"
|
366
|
+
```
|
367
|
+
|
368
|
+
The CLDR data set only includes 4 specific date formats, full, long, medium, and short, so you'll have to choose amongst them for the one that best fits your needs. Yes, it's limiting, but the 4 formats get the job done most of the time :)
|
369
|
+
|
370
|
+
### Running Tests (JS)
|
371
|
+
|
372
|
+
A JavaScript test suite comes with twitter-cldr-rb. You'll need to install the Qt libs to be able to run the suite, as it uses [jasmine](https://github.com/pivotal/jasmine-gem) and [jasmine-headless-webkit](http://johnbintz.github.com/jasmine-headless-webkit/).
|
373
|
+
|
374
|
+
1. Install qt (eg. `brew install qt`, `sudo apt-get install qt4`, etc)
|
375
|
+
2. Run `bundle`
|
376
|
+
3. Run `bundle exec rake js:test`
|
377
|
+
|
378
|
+
The tests are located in `js/spec` and look similar to RSpec tests.
|
379
|
+
|
331
380
|
## Authors
|
332
381
|
|
333
382
|
* Cameron C. Dutro: http://github.com/camertron
|
@@ -346,6 +395,5 @@ Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/L
|
|
346
395
|
|
347
396
|
## Future Plans
|
348
397
|
|
349
|
-
*
|
350
|
-
* Implement algorithms for Unicode normalization, collation, and capitalization
|
398
|
+
* Implement Unicode Collation Algorithm (UCA) for sorting/searching.
|
351
399
|
* Patch Ruby 1.8 strings to provide better Unicode support (probably using pack and unpack).
|
data/Rakefile
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# Copyright 2012 Twitter, Inc
|
4
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
5
|
+
|
1
6
|
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
2
7
|
|
3
8
|
require 'bundler'
|
@@ -31,4 +36,24 @@ if RUBY_VERSION < '1.9.0'
|
|
31
36
|
t.pattern = './spec/**/*_spec.rb'
|
32
37
|
t.rcov_opts = %w(-T --sort coverage --exclude gems/,spec/)
|
33
38
|
end
|
39
|
+
end
|
40
|
+
|
41
|
+
namespace :js do
|
42
|
+
task :build do
|
43
|
+
require File.expand_path(File.join(File.dirname(__FILE__), %w[lib twitter_cldr]))
|
44
|
+
TwitterCldr.require_js
|
45
|
+
FileUtils.mkdir_p(TwitterCldr::Js.build_dir)
|
46
|
+
TwitterCldr::Js.output_dir = File.expand_path(ENV["OUTPUT_DIR"])
|
47
|
+
TwitterCldr::Js.make(:locales => TwitterCldr.supported_locales)
|
48
|
+
TwitterCldr::Js.install
|
49
|
+
end
|
50
|
+
|
51
|
+
task :test do
|
52
|
+
require File.expand_path(File.join(File.dirname(__FILE__), %w[lib twitter_cldr]))
|
53
|
+
TwitterCldr.require_js
|
54
|
+
FileUtils.mkdir_p(TwitterCldr::Js.build_dir)
|
55
|
+
TwitterCldr::Js.make(:locales => [:en])
|
56
|
+
TwitterCldr::Js.test
|
57
|
+
FileUtils.rm_rf(TwitterCldr::Js.build_dir)
|
58
|
+
end
|
34
59
|
end
|
data/lib/twitter_cldr.rb
CHANGED
@@ -4,12 +4,16 @@
|
|
4
4
|
# http://www.apache.org/licenses/LICENSE-2.0
|
5
5
|
|
6
6
|
$:.push(File.dirname(__FILE__))
|
7
|
+
$:.push(File.dirname(File.dirname(__FILE__)))
|
7
8
|
|
8
9
|
$KCODE = 'UTF-8' unless RUBY_VERSION >= '1.9.0'
|
9
10
|
|
10
11
|
require 'yaml'
|
11
12
|
require 'date'
|
12
13
|
require 'time'
|
14
|
+
require 'fileutils'
|
15
|
+
|
16
|
+
# gems
|
13
17
|
require 'forwardable'
|
14
18
|
|
15
19
|
require 'twitter_cldr/version'
|
@@ -39,6 +43,9 @@ module TwitterCldr
|
|
39
43
|
:'zh-tw' => :'zh-Hant'
|
40
44
|
}
|
41
45
|
|
46
|
+
# maps cldr locales to twitter locales
|
47
|
+
CLDR_LOCALE_MAP = TWITTER_LOCALE_MAP.invert
|
48
|
+
|
42
49
|
def_delegator :resources, :get_resource
|
43
50
|
def_delegator :resources, :get_locale_resource
|
44
51
|
|
@@ -64,6 +71,11 @@ module TwitterCldr
|
|
64
71
|
TWITTER_LOCALE_MAP.fetch(locale, locale)
|
65
72
|
end
|
66
73
|
|
74
|
+
def twitter_locale(locale)
|
75
|
+
locale = locale.to_sym
|
76
|
+
CLDR_LOCALE_MAP.fetch(locale, locale)
|
77
|
+
end
|
78
|
+
|
67
79
|
def supported_locales
|
68
80
|
@supported_locales ||= Dir.glob(File.join(RESOURCES_DIR, 'locales', '*')).map { |f| File.basename(f).to_sym }
|
69
81
|
end
|
@@ -73,8 +85,11 @@ module TwitterCldr
|
|
73
85
|
supported_locales.include?(locale) || supported_locales.include?(convert_locale(locale))
|
74
86
|
end
|
75
87
|
|
88
|
+
def require_js
|
89
|
+
require "js/lib/twitter_cldr_js"
|
90
|
+
end
|
76
91
|
end
|
77
92
|
|
78
93
|
end
|
79
94
|
|
80
|
-
require 'twitter_cldr/core_ext'
|
95
|
+
require 'twitter_cldr/core_ext'
|
@@ -2,6 +2,7 @@ require 'twitter_cldr/core_ext/localized_object'
|
|
2
2
|
require 'twitter_cldr/core_ext/calendars/datetime'
|
3
3
|
require 'twitter_cldr/core_ext/calendars/date'
|
4
4
|
require 'twitter_cldr/core_ext/calendars/time'
|
5
|
+
require 'twitter_cldr/core_ext/calendars/timespan'
|
5
6
|
require 'twitter_cldr/core_ext/numbers/localized_number'
|
6
7
|
require 'twitter_cldr/core_ext/numbers/bignum'
|
7
8
|
require 'twitter_cldr/core_ext/numbers/fixnum'
|
@@ -24,6 +24,20 @@ module TwitterCldr
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
+
def ago(options = {})
|
28
|
+
base_time = options[:base_time] || Time.now
|
29
|
+
seconds = self.to_time.base_obj.to_i - base_time.to_i
|
30
|
+
raise ArgumentError.new('Start date is after end date. Consider using "until" function.') if seconds > 0
|
31
|
+
TwitterCldr::Shared::LocalizedTimespan.new(seconds, @locale).to_s(options[:unit])
|
32
|
+
end
|
33
|
+
|
34
|
+
def until(options = {})
|
35
|
+
base_time = options[:base_time] || Time.now
|
36
|
+
seconds = self.to_time.base_obj.to_i - base_time.to_i
|
37
|
+
raise ArgumentError.new('End date is before start date. Consider using "ago" function.') if seconds < 0
|
38
|
+
TwitterCldr::Shared::LocalizedTimespan.new(seconds, @locale).to_s(options[:unit])
|
39
|
+
end
|
40
|
+
|
27
41
|
def to_s
|
28
42
|
to_default_s
|
29
43
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# Copyright 2012 Twitter, Inc
|
4
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
5
|
+
|
6
|
+
module TwitterCldr
|
7
|
+
module Shared
|
8
|
+
class LocalizedTimespan < LocalizedObject
|
9
|
+
|
10
|
+
def initialize(seconds, locale)
|
11
|
+
@formatter = TwitterCldr::Formatters::TimespanFormatter.new(:locale => locale)
|
12
|
+
@seconds = seconds
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s(unit = :default)
|
16
|
+
@formatter.format(@seconds, unit)
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def formatter_const
|
22
|
+
TwitterCldr::Formatters::TimespanFormatter
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -10,6 +10,7 @@ module TwitterCldr
|
|
10
10
|
autoload :DateTimeFormatter, 'twitter_cldr/formatters/calendars/datetime_formatter'
|
11
11
|
autoload :DateFormatter, 'twitter_cldr/formatters/calendars/date_formatter'
|
12
12
|
autoload :TimeFormatter, 'twitter_cldr/formatters/calendars/time_formatter'
|
13
|
+
autoload :TimespanFormatter, 'twitter_cldr/formatters/calendars/timespan_formatter'
|
13
14
|
|
14
15
|
autoload :Numbers, 'twitter_cldr/formatters/numbers'
|
15
16
|
autoload :NumberFormatter, 'twitter_cldr/formatters/numbers/number_formatter'
|
@@ -113,7 +113,7 @@ module TwitterCldr
|
|
113
113
|
@tokenizer.calendar[:months][:format][:wide][date.month]
|
114
114
|
when 5
|
115
115
|
raise NotImplementedError, 'requires cldr\'s "multiple inheritance"'
|
116
|
-
@tokenizer.calendar[:months][:format][:narrow][date.month]
|
116
|
+
# @tokenizer.calendar[:months][:format][:narrow][date.month]
|
117
117
|
else
|
118
118
|
# raise unknown date format
|
119
119
|
end
|
@@ -215,11 +215,6 @@ module TwitterCldr
|
|
215
215
|
def timezone_generic_non_location(time, pattern, length)
|
216
216
|
raise NotImplementedError, 'requires timezone translation data'
|
217
217
|
end
|
218
|
-
|
219
|
-
def round_to(number, precision)
|
220
|
-
factor = 10 ** precision
|
221
|
-
(number * factor).round.to_f / factor
|
222
|
-
end
|
223
218
|
end
|
224
219
|
end
|
225
220
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# Copyright 2012 Twitter, Inc
|
4
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
5
|
+
|
6
|
+
module TwitterCldr
|
7
|
+
module Formatters
|
8
|
+
class TimespanFormatter < Base
|
9
|
+
TIME_IN_SECONDS = {
|
10
|
+
:second => 1,
|
11
|
+
:minute => 60,
|
12
|
+
:hour => 3600,
|
13
|
+
:day => 86400,
|
14
|
+
:week => 604800,
|
15
|
+
:month => 2629743.83,
|
16
|
+
:year => 31556926
|
17
|
+
}
|
18
|
+
|
19
|
+
def initialize(options = {})
|
20
|
+
@tokenizer = TwitterCldr::Tokenizers::TimespanTokenizer.new(:locale => extract_locale(options))
|
21
|
+
end
|
22
|
+
|
23
|
+
def format(seconds, unit)
|
24
|
+
direction = seconds < 0 ? :ago : :until
|
25
|
+
|
26
|
+
if unit.nil? || unit == :default
|
27
|
+
unit = self.calculate_unit(seconds.abs)
|
28
|
+
end
|
29
|
+
|
30
|
+
number = calculate_time(seconds.abs, unit)
|
31
|
+
|
32
|
+
tokens = @tokenizer.tokens(:direction => direction, :unit => unit, :number => number)
|
33
|
+
strings = tokens.map { |token| token[:value]}
|
34
|
+
strings.join.gsub(/\{[0-9]\}/, number.to_s)
|
35
|
+
end
|
36
|
+
|
37
|
+
def calculate_unit(seconds)
|
38
|
+
if seconds < 30
|
39
|
+
:second
|
40
|
+
elsif seconds < 2670
|
41
|
+
:minute
|
42
|
+
elsif seconds < 86369
|
43
|
+
:hour
|
44
|
+
elsif seconds < 604800
|
45
|
+
:day
|
46
|
+
elsif seconds < 2591969
|
47
|
+
:week
|
48
|
+
elsif seconds < 31556926
|
49
|
+
:month
|
50
|
+
else
|
51
|
+
:year
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# 0 <-> 29 secs # => seconds
|
56
|
+
# 30 secs <-> 44 mins, 29 secs # => minutes
|
57
|
+
# 44 mins, 30 secs <-> 23 hrs, 59 mins, 29 secs # => hours
|
58
|
+
# 23 hrs, 59 mins, 29 secs <-> 29 days, 23 hrs, 59 mins, 29 secs # => days
|
59
|
+
# 29 days, 23 hrs, 59 mins, 29 secs <-> 1 yr minus 1 sec # => months
|
60
|
+
# 1 yr <-> max time or date # => years
|
61
|
+
def calculate_time(seconds, unit)
|
62
|
+
(seconds / TIME_IN_SECONDS[unit]).round.to_i
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -17,8 +17,8 @@ module TwitterCldr
|
|
17
17
|
def format(number, options = {})
|
18
18
|
opts = self.default_format_options_for(number).merge(options)
|
19
19
|
prefix, suffix, integer_format, fraction_format = *partition_tokens(self.get_tokens(number, opts))
|
20
|
+
|
20
21
|
int, fraction = parse_number(number, opts)
|
21
|
-
|
22
22
|
result = integer_format.apply(int, opts)
|
23
23
|
result << fraction_format.apply(fraction, opts) if fraction
|
24
24
|
"#{prefix.to_s}#{result}#{suffix.to_s}"
|
@@ -13,5 +13,6 @@ module TwitterCldr
|
|
13
13
|
autoload :DateTokenizer, 'twitter_cldr/tokenizers/calendars/date_tokenizer'
|
14
14
|
autoload :TimeTokenizer, 'twitter_cldr/tokenizers/calendars/time_tokenizer'
|
15
15
|
autoload :NumberTokenizer, 'twitter_cldr/tokenizers/numbers/number_tokenizer'
|
16
|
+
autoload :TimespanTokenizer, 'twitter_cldr/tokenizers/calendars/timespan_tokenizer'
|
16
17
|
end
|
17
18
|
end
|
@@ -41,13 +41,13 @@ module TwitterCldr
|
|
41
41
|
final
|
42
42
|
end
|
43
43
|
|
44
|
-
def tokens_for(
|
44
|
+
def tokens_for(path, type)
|
45
45
|
@@token_cache ||= {}
|
46
|
-
cache_key =
|
46
|
+
cache_key = compute_cache_key(@locale, path.join('.'), type)
|
47
47
|
|
48
48
|
unless @@token_cache.include?(cache_key)
|
49
49
|
result = []
|
50
|
-
tokens =
|
50
|
+
tokens = expand_pattern(pattern_for(traverse(path)), type)
|
51
51
|
|
52
52
|
tokens.each do |token|
|
53
53
|
if token.is_a?(Token) || token.is_a?(CompositeToken)
|
@@ -63,6 +63,21 @@ module TwitterCldr
|
|
63
63
|
@@token_cache[cache_key]
|
64
64
|
end
|
65
65
|
|
66
|
+
def tokens_with_placeholders_for(key)
|
67
|
+
@@token_cache ||= {}
|
68
|
+
cache_key = self.compute_cache_key(@locale, key, type)
|
69
|
+
|
70
|
+
unless @@token_cache.include?(cache_key)
|
71
|
+
result = []
|
72
|
+
tokens = self.tokenize_pattern(self.pattern_for(self.traverse(key)))
|
73
|
+
tokens.each do |token|
|
74
|
+
result << token
|
75
|
+
end
|
76
|
+
@@token_cache[cache_key] = result
|
77
|
+
end
|
78
|
+
@@token_cache[cache_key]
|
79
|
+
end
|
80
|
+
|
66
81
|
def compute_cache_key(*pieces)
|
67
82
|
if pieces && pieces.size > 0
|
68
83
|
pieces.join("|").hash
|
@@ -75,11 +90,10 @@ module TwitterCldr
|
|
75
90
|
@placeholders = {}
|
76
91
|
end
|
77
92
|
|
78
|
-
def traverse(
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
current[key]
|
93
|
+
def traverse(path, haystack = @resource)
|
94
|
+
path.inject(haystack) do |current, segment|
|
95
|
+
if current.is_a?(Hash) && current.has_key?(segment)
|
96
|
+
current[segment]
|
83
97
|
else
|
84
98
|
return
|
85
99
|
end
|
@@ -89,7 +103,7 @@ module TwitterCldr
|
|
89
103
|
def expand_pattern(format_str, type)
|
90
104
|
if format_str.is_a?(Symbol)
|
91
105
|
# symbols mean another path was given
|
92
|
-
self.expand_pattern(self.pattern_for(self.traverse(format_str)), type)
|
106
|
+
self.expand_pattern(self.pattern_for(self.traverse(format_str.to_s.split('.').map(&:to_sym))), type)
|
93
107
|
else
|
94
108
|
parts = tokenize_pattern(format_str)
|
95
109
|
final = []
|
@@ -9,14 +9,20 @@ module TwitterCldr
|
|
9
9
|
def initialize(options = {})
|
10
10
|
super(options)
|
11
11
|
@token_splitter_regex = /(\s*\'[\w\s-]+\'\s*|G{1,5}|y+|Y+|Q{1,4}|q{1,5}|M{1,5}|L{1,5}|d{1,2}|F{1}|E{1,5}|e{1,5}|c{1,5}|\#\{[^\}]+\})/
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
|
13
|
+
@token_type_regexes = [
|
14
|
+
{ :type => :composite, :regex => /^\#\{[^\}]+\}/, :content => /^\#\{([^\}]+)\}/ },
|
15
|
+
{ :type => :pattern, :regex => /^(?:G{1,5}|y+|Y+|Q{1,4}|q{1,5}|M{1,5}|L{1,5}|d{1,2}|F{1}|E{1,5}|e{1,5}|c{1,5})/ },
|
16
|
+
{ :type => :plaintext, :regex => // }
|
17
|
+
]
|
18
|
+
|
19
|
+
@paths = {
|
20
|
+
:default => [:formats, :date, :default],
|
21
|
+
:full => [:formats, :date, :full],
|
22
|
+
:long => [:formats, :date, :long],
|
23
|
+
:medium => [:formats, :date, :medium],
|
24
|
+
:short => [:formats, :date, :short]
|
25
|
+
}
|
20
26
|
end
|
21
27
|
|
22
28
|
protected
|
@@ -12,22 +12,26 @@ module TwitterCldr
|
|
12
12
|
|
13
13
|
def initialize(options = {})
|
14
14
|
@calendar_type = options[:calendar_type] || TwitterCldr::DEFAULT_CALENDAR_TYPE
|
15
|
+
|
15
16
|
@token_splitter_regex = //
|
16
|
-
@token_type_regexes
|
17
|
+
@token_type_regexes = [{ :type => :plaintext, :regex => // }]
|
18
|
+
|
19
|
+
@base_path = [:calendars]
|
17
20
|
|
18
|
-
@
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
@paths = {
|
22
|
+
:default => [:formats, :datetime, :default],
|
23
|
+
:full => [:formats, :datetime, :full],
|
24
|
+
:long => [:formats, :datetime, :long],
|
25
|
+
:medium => [:formats, :datetime, :medium],
|
26
|
+
:short => [:formats, :datetime, :short]
|
27
|
+
}
|
24
28
|
|
25
29
|
super(options)
|
26
30
|
end
|
27
31
|
|
28
32
|
def tokens(options = {})
|
29
33
|
type = options[:type] || :default
|
30
|
-
|
34
|
+
tokens_for(full_path_for(paths[type]), type)
|
31
35
|
end
|
32
36
|
|
33
37
|
def calendar
|
@@ -37,7 +41,7 @@ module TwitterCldr
|
|
37
41
|
protected
|
38
42
|
|
39
43
|
def full_path_for(path, calendar_type = @calendar_type)
|
40
|
-
|
44
|
+
@base_path + [calendar_type] + path
|
41
45
|
end
|
42
46
|
|
43
47
|
def init_resources
|