Module: I18nScrewdriver
- Defined in:
- lib/i18n_screwdriver.rb,
lib/i18n_screwdriver/version.rb,
lib/i18n_screwdriver/translation.rb,
lib/i18n_screwdriver/rails/engine.rb,
lib/i18n_screwdriver/translation_helper.rb
Defined Under Namespace
Modules: Rails, TranslationHelper
Classes: Translation
Constant Summary
collapse
- Error =
Class.new(StandardError)
- VERSION =
"9.0"
Class Method Summary
collapse
Class Method Details
.available_locales ⇒ Object
120
121
122
123
124
125
|
# File 'lib/i18n_screwdriver.rb', line 120
def self.available_locales
@available_locales ||= begin
raise Error, "Please set I18.available_locales" unless I18n.available_locales.count > 0
I18n.available_locales.map(&:to_s)
end
end
|
.default_locale ⇒ Object
113
114
115
116
117
118
|
# File 'lib/i18n_screwdriver.rb', line 113
def self.default_locale
@default_locale ||= begin
raise Error, "Please set I18.default_locale" unless I18n.default_locale.present?
I18n.default_locale.to_s
end
end
|
151
152
153
154
|
# File 'lib/i18n_screwdriver.rb', line 151
def self.(string)
namespace, text = string.split("|", 2)
text ? text : namespace
end
|
.file_with_translations_exists?(locale) ⇒ Boolean
18
19
20
|
# File 'lib/i18n_screwdriver.rb', line 18
def self.file_with_translations_exists?(locale)
File.exists?(filename_for_locale(locale))
end
|
.filename_for_locale(locale) ⇒ Object
9
10
11
|
# File 'lib/i18n_screwdriver.rb', line 9
def self.filename_for_locale(locale)
File.join("config", "locales", "application.#{locale}.yml")
end
|
.gather_translations ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/i18n_screwdriver.rb', line 94
def self.gather_translations
texts = []
symbols = []
Dir.glob("**/*.{haml,erb,slim,rb}").each do |file|
input = File.read(file)
texts.concat(grab_texts_to_be_translated(input))
symbols.concat(grab_symbols_to_be_translated(input))
end
Dir.glob("**/*.{js,coffee,hamlc,ejs,erb}").each do |file|
input = File.read(file)
texts.concat(grab_js_texts_to_be_translated(input))
end
translations = Hash[texts.uniq.map{ |text| [generate_key(text), (text)] }]
translations.merge(Hash[symbols.uniq.map{ |symbol| [generate_key(symbol), ""] }])
end
|
.generate_key(string) ⇒ Object
13
14
15
16
|
# File 'lib/i18n_screwdriver.rb', line 13
def self.generate_key(string)
string = string.strip
(string =~ /^:[a-z][a-z0-9_]*$/) ? string : Digest::MD5.hexdigest(string)
end
|
.grab_js_texts_to_be_translated(string) ⇒ Object
48
49
50
51
52
53
|
# File 'lib/i18n_screwdriver.rb', line 48
def self.grab_js_texts_to_be_translated(string)
[].tap do |texts|
texts.concat(string.scan(/\bI18n\.screw\(?\s*(?<!\\)"(.*?)(?<!\\)"/).map{ |v| unescape_string(v[0]) })
texts.concat(string.scan(/\bI18n\.screw\(?\s*(?<!\\)'(.*?)(?<!\\)'/).map{ |v| unescape_string(v[0]) })
end
end
|
.grab_symbols_to_be_translated(string) ⇒ Object
44
45
46
|
# File 'lib/i18n_screwdriver.rb', line 44
def self.grab_symbols_to_be_translated(string)
string.scan(/_\((:[a-z][a-z0-9_]*)\)/).flatten
end
|
.grab_texts_to_be_translated(string) ⇒ Object
37
38
39
40
41
42
|
# File 'lib/i18n_screwdriver.rb', line 37
def self.grab_texts_to_be_translated(string)
[].tap do |texts|
texts.concat(string.scan(/_\((?<!\\)"(.*?)(?<!\\)"\)/).map{ |v| unescape_string(v[0]) })
texts.concat(string.scan(/_\((?<!\\)'(.*?)(?<!\\)'\)/).map{ |v| unescape_string(v[0]) })
end
end
|
.in_utf8(hash) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/i18n_screwdriver.rb', line 55
def self.in_utf8(hash)
{}.tap do |result|
hash.sort.each do |k, v|
result[k.encode('UTF-8')] = (v || "").encode('UTF-8')
end
end
end
|
.load_translations(locale) ⇒ Object
22
23
24
25
26
|
# File 'lib/i18n_screwdriver.rb', line 22
def self.load_translations(locale)
path = filename_for_locale(locale)
raise Error, "File #{path} not found!" unless File.exists?(path)
sanitize_hash(YAML.load_file(path)[locale])
end
|
.sanitize_hash(hash) ⇒ Object
139
140
141
142
143
|
# File 'lib/i18n_screwdriver.rb', line 139
def self.sanitize_hash(hash)
{}.tap do |new_hash|
hash.each{ |k, v| new_hash[k.to_s] = v.to_s }
end
end
|
.translate(string, options = {}) ⇒ Object
145
146
147
148
149
|
# File 'lib/i18n_screwdriver.rb', line 145
def self.translate(string, options = {})
I18n.translate!(generate_key(string), options)
rescue I18n::MissingTranslationData
I18n.translate(string, options)
end
|
.unescape_string(string) ⇒ Object
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
|
# File 'lib/i18n_screwdriver.rb', line 63
def self.unescape_string(string)
"".tap do |result|
in_backslash = false
string.each_char do |char|
if in_backslash
case char
when 'r'
result << "\r"
when 'n'
result << "\n"
when 't'
result << "\t"
when '"', "'", '\\'
result << char
else
result << '\\'
result << char
end
in_backslash = false
else
case char
when '\\'
in_backslash = true
else
result << char
end
end
end
end
end
|
.update_translations_file(locale, translations) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/i18n_screwdriver.rb', line 127
def self.update_translations_file(locale, translations)
existing_translations = file_with_translations_exists?(locale) ? load_translations(locale) : {}
existing_translations.select!{ |k| translations.has_key?(k) }
translations.each do |k, v|
next if existing_translations[k]
existing_translations[k] = (default_locale == locale) ? v : nil
end
write_translations(locale, existing_translations)
end
|
.write_translations(locale, translations) ⇒ Object
28
29
30
31
32
33
34
35
|
# File 'lib/i18n_screwdriver.rb', line 28
def self.write_translations(locale, translations)
File.open(filename_for_locale(locale), "w") do |file|
file.puts "# use rake task i18n:update to generate this file"
file.puts
file.puts({locale => in_utf8(translations)}.to_yaml(:line_width => -1))
file.puts
end
end
|