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
- VERSION =
"5.0"
Class Method Summary
collapse
Class Method Details
.filename(locale) ⇒ Object
7
8
9
|
# File 'lib/i18n_screwdriver.rb', line 7
def self.filename(locale)
"config/locales/application.#{locale}.yml"
end
|
.for_key(string) ⇒ Object
11
12
13
|
# File 'lib/i18n_screwdriver.rb', line 11
def self.for_key(string)
Digest::MD5.hexdigest(string.strip)
end
|
.grab_texts_to_be_translated(string) ⇒ Object
30
31
32
33
34
35
|
# File 'lib/i18n_screwdriver.rb', line 30
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
37
38
39
40
41
42
43
|
# File 'lib/i18n_screwdriver.rb', line 37
def self.in_utf8(hash)
{}.tap do |result|
hash.each do |k, v|
result[k.encode('UTF-8')] = (v || "").encode('UTF-8')
end
end
end
|
.load_translations(locale) ⇒ Object
15
16
17
18
19
|
# File 'lib/i18n_screwdriver.rb', line 15
def self.load_translations(locale)
filename = self.filename(locale)
raise "File #{filename} not found!" unless File.exists?(filename)
YAML.load_file(filename)[locale]
end
|
.unescape_string(string) ⇒ Object
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
|
# File 'lib/i18n_screwdriver.rb', line 45
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
|
.write_translations(locale, translations) ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/i18n_screwdriver.rb', line 21
def self.write_translations(locale, translations)
File.open(filename(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)
file.puts
end
end
|