/
twitter_cldr.rb
144 lines (110 loc) · 3.62 KB
/
twitter_cldr.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# encoding: UTF-8
# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0
$KCODE = 'UTF-8' unless RUBY_VERSION >= '1.9.0'
require 'yaml'
require 'date'
require 'time'
require 'fileutils'
# gems
require 'forwardable'
require 'twitter_cldr/version'
Enumerator = Enumerable::Enumerator unless defined?(Enumerator)
module TwitterCldr
autoload :Formatters, 'twitter_cldr/formatters'
autoload :Collation, 'twitter_cldr/collation'
autoload :Localized, 'twitter_cldr/localized'
autoload :Normalization, 'twitter_cldr/normalization'
autoload :Resources, 'twitter_cldr/resources'
autoload :Shared, 'twitter_cldr/shared'
autoload :Tokenizers, 'twitter_cldr/tokenizers'
autoload :Utils, 'twitter_cldr/utils'
extend SingleForwardable
# version of CLDR that was used for generating YAML files in the resources/ directory
CLDR_VERSION = '21.0' # release date: 2012-02-10
DEFAULT_LOCALE = :en
DEFAULT_CALENDAR_TYPE = :gregorian
RESOURCES_DIR = File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'resources')
# maps twitter locales to cldr locales
TWITTER_LOCALE_MAP = {
:msa => :ms,
:'zh-cn' => :zh,
:'zh-tw' => :'zh-Hant',
:no => :nb
}
# maps cldr locales to twitter locales
CLDR_LOCALE_MAP = TWITTER_LOCALE_MAP.invert
def_delegator :resources, :get_resource
def_delegator :resources, :get_locale_resource
class << self
attr_accessor :locale
def resources
@resources ||= TwitterCldr::Resources::Loader.new
end
def locale=(new_locale)
@locale = new_locale
end
def locale
locale = supported_locale?(@locale) ? @locale : find_fallback
locale = DEFAULT_LOCALE if locale.to_s.empty?
(supported_locale?(locale) ? locale : DEFAULT_LOCALE).to_sym
end
def with_locale(locale)
raise "Unsupported locale" unless supported_locale?(locale)
old_locale = @locale
@locale = locale
result = yield
@locale = old_locale
result
end
def register_locale_fallback(proc_or_locale)
case proc_or_locale.class.to_s
when "Symbol", "String", "Proc"
locale_fallbacks << proc_or_locale
else
raise "A locale fallback must be of type String, Symbol, or Proc."
end
nil
end
def reset_locale_fallbacks
locale_fallbacks.clear
TwitterCldr.register_locale_fallback(lambda { I18n.locale if defined?(I18n) && I18n.respond_to?(:locale) })
TwitterCldr.register_locale_fallback(lambda { FastGettext.locale if defined?(FastGettext) && FastGettext.respond_to?(:locale) })
end
def locale_fallbacks
@locale_fallbacks ||= []
end
def convert_locale(locale)
locale = locale.to_sym if locale.respond_to?(:to_sym)
TWITTER_LOCALE_MAP.fetch(locale, locale)
end
def twitter_locale(locale)
locale = locale.to_sym
CLDR_LOCALE_MAP.fetch(locale, locale)
end
def supported_locales
@supported_locales ||= Dir.glob(File.join(RESOURCES_DIR, 'locales', '*')).map { |f| File.basename(f).to_sym }
end
def supported_locale?(locale)
!!locale && supported_locales.include?(convert_locale(locale))
end
protected
def find_fallback
(locale_fallbacks.size - 1).downto(0).each do |i|
result = if locale_fallbacks[i].is_a?(Proc)
begin
locale_fallbacks[i].call
rescue
nil
end
else
locale_fallbacks[i]
end
return result if result
end
nil
end
end
end
TwitterCldr.reset_locale_fallbacks
require 'twitter_cldr/core_ext'