er18ern 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/er18ern/version.rb +1 -1
  2. data/lib/er18ern.rb +222 -0
  3. metadata +3 -3
@@ -1,3 +1,3 @@
1
1
  module Er18Ern
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/er18ern.rb CHANGED
@@ -0,0 +1,222 @@
1
+ module Er18Ern
2
+
3
+ class LocaleCookie
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ request = Rack::Request.new(env)
10
+ request_locale = request.params["locale"] || request.cookies["locale"]
11
+ if request_locale && I18n.available_locales.include?(request_locale.to_sym)
12
+ I18n.locale = request_locale
13
+ end
14
+ status, headers, body = @app.call(env)
15
+ cookie = {:value => I18n.locale, :expires => Time.now + 10.days, :domain => request.host.gsub(/^[^\.]*/,"")}
16
+ Rack::Utils.set_cookie_header!(headers, "locale", cookie)
17
+ [status, headers, body]
18
+ end
19
+ end
20
+
21
+ class JustRaise
22
+ def call(exception, locale, key, options)
23
+ raise "Missing Translation: #{exception}"
24
+ end
25
+ end
26
+
27
+ class RailsApp
28
+ def self.setup!
29
+ if ["test", "development"].include?(Rails.env)
30
+ I18n.exception_handler = JustRaise.new
31
+ end
32
+ Array.class_eval do
33
+ def to_br_sentence
34
+ case length
35
+ when 0
36
+ ""
37
+ when 1
38
+ self[0].to_s.dup
39
+ when 2
40
+ two_words_connector = I18n.translate(:'support.array.br_two_words_connector')
41
+ "#{self[0]}#{two_words_connector}#{self[1]}".html_safe
42
+ else
43
+ words_connector = I18n.translate(:'support.array.br_words_connector')
44
+ last_word_connector = I18n.translate(:'support.array.br_last_word_connector')
45
+ "#{self[0...-1].join(words_connector)}#{last_word_connector}#{self[-1]}".html_safe
46
+ end
47
+ end
48
+ end
49
+ ActiveModel::Errors.class_eval do
50
+ def full_message(attribute, message)
51
+ return message if attribute == :base
52
+ attr_name = attribute.to_s.tr('.', '_').humanize
53
+ attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
54
+ I18n.t(:"errors.formats.attributes.#{attribute}", {
55
+ :default => [:"errors.format","%{attribute} %{message}"],
56
+ :attribute => attr_name,
57
+ :message => message
58
+ })
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ require 'ermahgerd'
65
+ module ImprovedErmahgerd
66
+
67
+ def self.translate(words)
68
+ words.gsub(/([{]?)([&]?)\w+/) do |word|
69
+ if $1 == "{" || $2 == "&"
70
+ word
71
+ else
72
+ translate_word(word.upcase)
73
+ end
74
+ end
75
+ end
76
+
77
+ def self.translate_word(word)
78
+ case word
79
+ when "ENGINE"
80
+ "ERNGIN"
81
+ when "YARD"
82
+ "YERD"
83
+ else
84
+ Ermahgerd.translate_word(word)
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ class Translator
91
+ attr_accessor :locales_dir, :google_translator_hax_locale_dirs
92
+
93
+ def en_source_yaml
94
+ @en_source_yaml ||= YAML::load_file(File.expand_path('en.yml', self.locales_dir))
95
+ end
96
+
97
+ def generate_ermahgerd!
98
+ traverse = Proc.new do |x, translator|
99
+ if x.is_a?(Hash)
100
+ result = {}
101
+ x.each do |k, v|
102
+ result[k] = traverse.call(v, translator)
103
+ end
104
+ result
105
+ else
106
+ translator.call(x)
107
+ end
108
+ end
109
+
110
+ errm = {}
111
+ errm['ermahgerd'] = en_source_yaml["en"]
112
+
113
+ #save ermahgerd
114
+ File.open(File.expand_path('ermahgerd.yml', self.locales_dir), "w") do |fp|
115
+ fp.write(traverse.call(errm, lambda{|x| ImprovedErmahgerd.translate(x)}).to_yaml)
116
+ end
117
+ end
118
+
119
+ def resave_en!
120
+ #re-save EN
121
+ File.open(File.expand_path('en.yml', self.locales_dir), "w") do |fp|
122
+ fp.write(en_source_yaml.to_yaml)
123
+ end
124
+ end
125
+
126
+ def found_keys
127
+ @found_keys ||= begin
128
+ found_keys = {}
129
+ key_collector = Proc.new do |x, key_prefix|
130
+ if x.is_a?(Hash)
131
+ result = {}
132
+ x.each do |k, v|
133
+ result[k] = key_collector.call(v, "#{key_prefix}.#{k}")
134
+ end
135
+ result
136
+ else
137
+ found_keys[key_prefix] = x
138
+ end
139
+ end
140
+ key_collector.call(en_source_yaml["en"], "")
141
+ found_keys
142
+ end
143
+ end
144
+
145
+ def generate_google_translations!
146
+ vals = found_keys.values.uniq.map do |val|
147
+ strip_string(val)
148
+ end
149
+
150
+ File.open(File.expand_path('GOOGLE_TRANSLATE_INPUT', self.google_translator_hax_locale_dirs), "w") do |fp|
151
+ fp.write(vals.join("\n---\n"))
152
+ end
153
+ end
154
+
155
+ def save_engrish!
156
+ enstuff = File.read(File.expand_path('GOOGLE_TRANSLATE_INPUT', self.google_translator_hax_locale_dirs)).split("\n---\n")
157
+ engrishstuff = File.read(File.expand_path('GOOGLE_TRANSLATE_AGAIN', self.google_translator_hax_locale_dirs)).split("\n---\n")
158
+ engrish = doitstuff(enstuff, found_keys, "engrish", engrishstuff)
159
+
160
+ File.open(File.expand_path('engrish.yml', self.locales_dir), "w") do |fp|
161
+ fp.write(engrish.to_yaml)
162
+ end
163
+ end
164
+
165
+ def save_jp!
166
+ enstuff = File.read(File.expand_path('GOOGLE_TRANSLATE_INPUT', self.google_translator_hax_locale_dirs)).split("\n---\n")
167
+ jpstuff = File.read(File.expand_path('GOOGLE_TRANSLATE_OUTPUT', self.google_translator_hax_locale_dirs)).split("\n---\n")
168
+ jp = doitstuff(enstuff, found_keys, "jp", jpstuff)
169
+
170
+ File.open(File.expand_path('jp.yml', self.locales_dir), "w") do |fp|
171
+ fp.write(jp.to_yaml)
172
+ end
173
+ end
174
+
175
+ private
176
+
177
+ def doitstuff(enstuff, found_keys, locale, stuff)
178
+ lookup = {}
179
+ found_keys.each do |k, v|
180
+ if index = enstuff.index(strip_string(v))
181
+ lookup[k] = stuff && stuff[index]
182
+ end
183
+ end
184
+
185
+ jp = {locale => {}}
186
+ lookup.each do |k, v|
187
+ hash = jp[locale]
188
+ last = nil
189
+ k.split(".").each do |key_part|
190
+ unless key_part.empty?
191
+ last_hash = hash
192
+ last = Proc.new do |str|
193
+ en_string = found_keys[k]
194
+ en_string_parts = strip_string(en_string).split("***").reject{|x| x.strip.empty? }
195
+ jp_string_parts = str.split("***").reject(&:empty?)
196
+ en_string_parts.each_with_index do |part, index|
197
+ jp_corresponding = jp_string_parts[index] || "" # "MISSING TRANSLATION"
198
+ en_string = en_string.gsub(part.to_s, jp_corresponding)
199
+ end
200
+ last_hash[key_part] = en_string
201
+ end
202
+ hash[key_part] ||= {}
203
+ hash = hash[key_part]
204
+ end
205
+ end
206
+ last.call(v)
207
+ end
208
+ jp
209
+ end
210
+
211
+ def strip_string(str)
212
+ str.gsub(/([%]?)([&]?)([{]?)\w+([;]?)([}]?)/) do |word|
213
+ if $1 == "%" || $2 == "&"
214
+ "***"
215
+ else
216
+ word
217
+ end
218
+ end
219
+ end
220
+
221
+ end
222
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: er18ern
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jacob & Others too ashamed to admit it