/
localization.rb
105 lines (84 loc) · 2.68 KB
/
localization.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
# encoding: UTF-8
require 'stringex/localization/default_conversions'
module Stringex
module Localization
include DefaultConversions
class << self
attr_writer :translations, :backend
def translations
# Set up hash like translations[:en][:transliterations]["é"]
@translations ||= Hash.new { |k, v| k[v] = Hash.new({}) }
end
def backend
@backend ||= defined?(I18n) ? :i18n : :internal
end
def store_translations(locale, scope, data)
if backend == :i18n
I18n.backend.store_translations(locale, { :stringex => { scope => data } })
else
self.translations[locale.to_sym][scope.to_sym] = Hash[data.map { |k, v| [k.to_sym, v] }] # Symbolize keys
end
end
def translate(scope, key, options = {})
return if key == "." # I18n doesn't support dots as translation keys so we don't either
locale = options[:locale] || self.locale
translation = initial_translation(scope, key, locale)
return translation unless translation.to_s.empty?
if locale != default_locale
translate scope, key, options.merge(:locale => default_locale)
else
default_conversion(scope, key) || options[:default]
end
end
def locale
if @locale
@locale
elsif defined?(I18n)
I18n.locale
else
default_locale
end
end
def locale=(new_locale)
@locale = new_locale.to_sym
end
def default_locale
if @default_locale
@default_locale
elsif defined?(I18n)
I18n.default_locale
else
:en
end
end
def default_locale=(new_locale)
@default_locale = @locale = new_locale.to_sym
end
def with_locale(new_locale, &block)
new_locale = default_locale if new_locale == :default
original_locale = locale
self.locale = new_locale
block.call
self.locale = original_locale
end
def with_default_locale(&block)
with_locale default_locale, &block
end
def reset!
@backend = @translations = @locale = @default_locale = nil
end
private
def initial_translation(scope, key, locale)
if backend == :i18n
I18n.translate(key, :scope => [:stringex, scope], :locale => locale, :default => "")
else
translations[locale][scope][key.to_sym]
end
end
def default_conversion(scope, key)
return unless Stringex::Localization::DefaultConversions.respond_to?(scope)
Stringex::Localization::DefaultConversions.send(scope)[key]
end
end
end
end