gettext_simple 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/gettext_simple.gemspec +1 -1
- data/lib/gettext_simple.rb +32 -14
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e43070b335b7174bf71fbb327f5fd561e4ea3138
|
4
|
+
data.tar.gz: 71c72e671bdb466f1ab9a5d4dd4ff04855e176e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c153f4b4a9b558445d11ebe3f8b0457ae74e5b0a27a120ff94e720c522e033fd4595d8632097db1f79318c2cc5ee9e5471bc0adbe413f20f356c68993806b6a
|
7
|
+
data.tar.gz: 4509cddc233d9f337c0216c29d29aaeac21337a232a925853255ac461767d97e4aecd4d6405e9bfd10fcc910c122591098dba04fc80d953f824675dcf27f1597
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
data/gettext_simple.gemspec
CHANGED
data/lib/gettext_simple.rb
CHANGED
@@ -8,6 +8,7 @@ class GettextSimple
|
|
8
8
|
:default_locale => "en"
|
9
9
|
}.merge(args)
|
10
10
|
@locales = {}
|
11
|
+
@debug = @args[:debug]
|
11
12
|
|
12
13
|
# To avoid doing a hash lookup on every translate.
|
13
14
|
@i18n = @args[:i18n]
|
@@ -37,20 +38,26 @@ class GettextSimple
|
|
37
38
|
check_folders = ["LC_MESSAGES", "LC_ALL"]
|
38
39
|
|
39
40
|
Dir.foreach(dir) do |file|
|
41
|
+
debug "New locale folder found: '#{file}'." if @debug
|
42
|
+
|
40
43
|
fn = "#{dir}/#{file}"
|
41
|
-
if File.directory?(fn)
|
42
|
-
|
44
|
+
if !File.directory?(fn) || !file.match(/^[a-z]{2}/)
|
45
|
+
debug "Skipping: '#{file}'." if @debug
|
46
|
+
next
|
47
|
+
end
|
48
|
+
|
49
|
+
@locales[file] = {} unless @locales[file]
|
50
|
+
|
51
|
+
check_folders.each do |fname|
|
52
|
+
fpath = "#{dir}/#{file}/#{fname}"
|
53
|
+
next if !File.exists?(fpath) || !File.directory?(fpath)
|
54
|
+
debug "Found subfolder in '#{file}': '#{fname}'." if @debug
|
43
55
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
if pofile.match(/\.po$/)
|
50
|
-
pofn = "#{dir}/#{file}/#{fname}/#{pofile}"
|
51
|
-
scan_pofile(file, pofn)
|
52
|
-
end
|
53
|
-
end
|
56
|
+
Dir.foreach(fpath) do |pofile|
|
57
|
+
if pofile.match(/\.po$/)
|
58
|
+
debug "Starting to parse '#{pofile}'." if @debug
|
59
|
+
pofn = "#{dir}/#{file}/#{fname}/#{pofile}"
|
60
|
+
scan_pofile(file, pofn)
|
54
61
|
end
|
55
62
|
end
|
56
63
|
end
|
@@ -78,12 +85,14 @@ class GettextSimple
|
|
78
85
|
end
|
79
86
|
end
|
80
87
|
|
88
|
+
debug "Translation with locale '#{locale}' and replaces '#{replaces}': '#{str}' to '#{translated_str}'." if @debug
|
89
|
+
|
81
90
|
return translated_str
|
82
91
|
end
|
83
92
|
|
84
93
|
def translate(str, replaces = nil)
|
85
94
|
if @i18n
|
86
|
-
locale = I18n.locale
|
95
|
+
locale = I18n.locale.to_s
|
87
96
|
elsif locale = Thread.current[:gettext_simple_locale]
|
88
97
|
# Locale already set through condition.
|
89
98
|
else
|
@@ -99,6 +108,10 @@ class GettextSimple
|
|
99
108
|
end
|
100
109
|
|
101
110
|
private
|
111
|
+
|
112
|
+
def debug(str)
|
113
|
+
$stderr.puts str if @debug
|
114
|
+
end
|
102
115
|
|
103
116
|
def scan_pofile(locale, filepath)
|
104
117
|
current_id = nil
|
@@ -107,6 +120,7 @@ private
|
|
107
120
|
reading_id = false
|
108
121
|
reading_translation = false
|
109
122
|
|
123
|
+
debug "Opening file for parsing: '#{filepath}' in locale '#{locale}'." if @debug
|
110
124
|
File.open(filepath, "r", :encoding => @args[:encoding]) do |fp|
|
111
125
|
fp.each_line do |line|
|
112
126
|
if match = line.match(/^(msgid|msgstr)\s+"(.*)"$/)
|
@@ -150,6 +164,10 @@ private
|
|
150
164
|
|
151
165
|
def add_translation(locale, key, val)
|
152
166
|
raise "No such language: '#{locale}'." unless @locales.key?(locale)
|
153
|
-
|
167
|
+
|
168
|
+
if !key.to_s.empty? && !val.to_s.empty?
|
169
|
+
debug "Found translation for locale '#{locale}' '#{key}' which is: '#{val}'." if @debug
|
170
|
+
@locales[locale][key] = val
|
171
|
+
end
|
154
172
|
end
|
155
173
|
end
|