Class: Cheepub::I18n
- Inherits:
-
Object
show all
- Defined in:
- lib/cheepub/i18n.rb
Defined Under Namespace
Classes: InterpolateError, InvalidLocaleData
Constant Summary
collapse
- INTERPOLATION_PATTERN =
%r<(?:%%|%\{([A-Za-z0-9_]+)\})>
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ I18n
Returns a new instance of I18n.
56
57
58
59
60
|
# File 'lib/cheepub/i18n.rb', line 56
def initialize
@translations = {}
@available_locales = [:ja, :en]
@default_locales = :en
end
|
Instance Attribute Details
#available_locales ⇒ Object
Returns the value of attribute available_locales.
54
55
56
|
# File 'lib/cheepub/i18n.rb', line 54
def available_locales
@available_locales
end
|
#default_locale ⇒ Object
Returns the value of attribute default_locale.
54
55
56
|
# File 'lib/cheepub/i18n.rb', line 54
def default_locale
@default_locale
end
|
#load_path ⇒ Object
Returns the value of attribute load_path.
54
55
56
|
# File 'lib/cheepub/i18n.rb', line 54
def load_path
@load_path
end
|
Class Method Details
.available_locales ⇒ Object
39
40
41
|
# File 'lib/cheepub/i18n.rb', line 39
def self.available_locales
@i18n.available_locales
end
|
.available_locales=(locales) ⇒ Object
35
36
37
|
# File 'lib/cheepub/i18n.rb', line 35
def self.available_locales=(locales)
@i18n.available_locales = locales
end
|
.default_locale=(locale) ⇒ Object
43
44
45
|
# File 'lib/cheepub/i18n.rb', line 43
def self.default_locale=(locale)
@i18n.default_locale = locale
end
|
.load_path ⇒ Object
30
31
32
33
|
# File 'lib/cheepub/i18n.rb', line 30
def self.load_path
@i18n.load_path ||= []
@i18n.load_path
end
|
.load_path=(path) ⇒ Object
26
27
28
|
# File 'lib/cheepub/i18n.rb', line 26
def self.load_path=(path)
@i18n.load_path = path
end
|
.t(*args) ⇒ Object
47
48
49
50
|
# File 'lib/cheepub/i18n.rb', line 47
def self.t(*args)
key = args.shift
@i18n.translate(key, args)
end
|
Instance Method Details
#interpolate(str, values) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/cheepub/i18n.rb', line 110
def interpolate(str, values)
return "" if str.nil?
str.gsub(INTERPOLATION_PATTERN) do |match|
if match == '%%'
'%'
else
key = $1.to_sym
if values.key?(key)
values[key]
else
raise I18n::InterpolateError.new(key, values, str)
end
end
end
end
|
#load_data(file) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/cheepub/i18n.rb', line 67
def load_data(file)
begin
data = nil
if file.kind_of?(Hash)
data = file
else
content = File.read(file)
data = YAML.safe_load(content)
end
data.symbolize_keys!
pp data
data.each { |locale, d| store_translations(locale, d || {}) }
rescue => e
raise InvalidLocaleData.new(file, e.inspect)
end
end
|
#load_files(*files) ⇒ Object
62
63
64
65
|
# File 'lib/cheepub/i18n.rb', line 62
def load_files(*files)
filenames = I18n.load_path if filenames.empty?
filenames.each { |filename| load_data(filename) }
end
|
#lookup(locale, key) ⇒ Object
106
107
108
|
# File 'lib/cheepub/i18n.rb', line 106
def lookup(locale, key)
translations[locale][key] rescue nil
end
|
#store_translations(locale, data) ⇒ Object
84
85
86
87
88
89
90
91
92
|
# File 'lib/cheepub/i18n.rb', line 84
def store_translations(locale, data)
locale = locale.to_sym
if !I18n.available_locales.include?(locale)
return data
end
translations[locale] ||= {}
data.symbolize_keys!
translations[locale].deep_merge!(data)
end
|
#translate(key, options = {}) ⇒ Object
Also known as:
t
98
99
100
101
102
|
# File 'lib/cheepub/i18n.rb', line 98
def translate(key, options = {})
locale ||= @default_locale
entry = lookup(locale, key)
interpolate(entry, options)
end
|
#translations ⇒ Object
94
95
96
|
# File 'lib/cheepub/i18n.rb', line 94
def translations
@translations
end
|