air18n 0.1.25 → 0.1.26

Sign up to get free protection for your applications and to get access to all the features.
@@ -224,5 +224,35 @@ module Air18n
224
224
 
225
225
  result
226
226
  end
227
+
228
+ # If there is a translation of given key in given, or a less-specific
229
+ # fallback locale, returns the most specific locale that has a translation.
230
+ # Returns nil otherwise.
231
+ #
232
+ # For "guessed" specific locales like British English, returns the specific
233
+ # locale only if there is a manually-written translation for that locale.
234
+ #
235
+ # This is useful for knowing whether or not there is a translation for a
236
+ # key in a locale. It can also be used to determine whether a translation comes
237
+ # from a fallback locale or not.
238
+ def has_translation?(key, locale)
239
+ if key.blank? || !key.is_a?(String)
240
+ return false
241
+ end
242
+
243
+ init_translations(locale)
244
+
245
+ for fallback_locale in I18n.fallbacks_for(locale, :exclude_default => true)
246
+ if @translation_data.include?(fallback_locale) && @translation_data[fallback_locale].include?(key)
247
+ return fallback_locale
248
+ end
249
+
250
+ if fallback_locale == I18n.default_locale
251
+ return fallback_locale
252
+ end
253
+ end
254
+
255
+ return nil
256
+ end
227
257
  end
228
258
  end
@@ -294,5 +294,21 @@ module Air18n
294
294
 
295
295
  result
296
296
  end
297
+
298
+ # If there is a translation of given key in given, or a less-specific
299
+ # fallback locale, returns the most specific locale that has a translation.
300
+ # Returns nil otherwise.
301
+ #
302
+ # For "guessed" specific locales like British English, returns the specific
303
+ # locale only if there is a manually-written translation for that locale.
304
+ # Otherwise, if the given locale falls back to the default locale, returns
305
+ # the default locale.
306
+ #
307
+ # This is useful for knowing whether or not there is a translation for a
308
+ # key in a locale. It can also be used to determine whether a translation comes
309
+ # from a fallback locale or not.
310
+ def has_translation?(key, locale)
311
+ @air18n_backend.has_translation?(key, locale)
312
+ end
297
313
  end
298
314
  end
@@ -1,3 +1,3 @@
1
1
  module Air18n
2
- VERSION = "0.1.25"
2
+ VERSION = "0.1.26"
3
3
  end
@@ -136,6 +136,33 @@ describe Air18n::Backend do
136
136
  end
137
137
  end
138
138
 
139
+ context 'has_translation?' do
140
+ it 'should fallback to less specific locale' do
141
+ @phrase1 = FactoryGirl.create(:phrase, :key => 'has_translation?, key 1', :value => 'value 1')
142
+ FactoryGirl.create(:phrase_translation, :phrase => @phrase1, :value => 'Spanish Spanish value 1', :locale => :es)
143
+ FactoryGirl.create(:phrase_translation, :phrase => @phrase1, :value => 'Latin American Spanish value 1', :locale => :"es-419")
144
+ @phrase2 = FactoryGirl.create(:phrase, :key => 'has_translation?, key 2', :value => 'value 2')
145
+ FactoryGirl.create(:phrase_translation, :phrase => @phrase2, :value => 'Spanish Spanish value 2', :locale => :es)
146
+
147
+ @backend = Air18n::Backend.new
148
+ @backend.lookup(:es, 'has_translation?, key 1', @scope).should == 'Spanish Spanish value 1'
149
+ @backend.lookup(:"es-419", 'has_translation?, key 1', @scope).should == 'Latin American Spanish value 1'
150
+ @backend.lookup(:es, 'has_translation?, key 2', @scope).should == 'Spanish Spanish value 2'
151
+ @backend.lookup(:"es-419", 'has_translation?, key 2', @scope).should == 'Spanish Spanish value 2'
152
+
153
+ @backend.has_translation?('has_translation?, key 1', :"es-419").should == :"es-419"
154
+ @backend.has_translation?('has_translation?, key 1', :es).should == :es
155
+ @backend.has_translation?('has_translation?, key 2', :"es-419").should == :es
156
+ @backend.has_translation?('has_translation?, key 2', :es).should == :es
157
+
158
+ @backend.has_translation?('has_translation?, key 2', :"en-GB").should == :en
159
+ @backend.has_translation?('has_translation?, key 2', :en).should == :en
160
+
161
+ @backend.has_translation?('has_translation?, key 2', :"de-AT").should == nil
162
+ @backend.has_translation?('has_translation?, key 2', :de).should == nil
163
+ end
164
+ end
165
+
139
166
  context 'Language variants' do
140
167
  before :all do
141
168
  I18n.reset(:en)
@@ -179,8 +179,7 @@ describe Air18n::PhraseTranslation do
179
179
  it 'should keep track of translated and verified word count in new regime' do
180
180
  @user1 = 3
181
181
  @user2 = 4
182
- @phrase1 = FactoryGirl.create(:phrase, :value => 'two words')
183
- @phrase2 = FactoryGirl.create(:phrase, :value => 'three long words')
182
+ @phrase1 = FactoryGirl.create(:phrase, :key => 'new regime payments key 1', :value => 'two words')
184
183
 
185
184
  without_timestamping_of Air18n::PhraseTranslation do
186
185
  @phrase1.value = "one two three four five six seven eight"
@@ -210,33 +209,35 @@ describe Air18n::PhraseTranslation do
210
209
  # Translator B is paid for verifying phrase Y -- 8 words
211
210
  # Translator B is paid for translating phrase Y -- 13 words
212
211
 
213
- data.should include(
212
+ data.first.should include(
214
213
  {:year=>2012,
215
214
  :month=>10,
216
215
  :locale=>"es",
217
- :user_id=>3,
218
- :activity=>
216
+ :user_id=>3}
217
+ )
218
+ data.first[:activity].should include(
219
219
  {:num_translations=>2,
220
220
  :num_verifications=>0,
221
221
  :word_count_translations=>21,
222
222
  :word_count_verifications=>0,
223
223
  :translated_keys=>
224
- [["example.key.4", "38a72c02b5febe4975fdcf19d32e412d"],
225
- ["example.key.4", "bef8ae7ec7a9d0145615a1dd8e902e43"]],
226
- :verified_keys=>[]}}
224
+ [["new regime payments key 1", "38a72c02b5febe4975fdcf19d32e412d"],
225
+ ["new regime payments key 1", "bef8ae7ec7a9d0145615a1dd8e902e43"]],
226
+ :verified_keys=>[]}
227
227
  )
228
- data.should include(
228
+ data.second.should include(
229
229
  {:year=>2012,
230
230
  :month=>10,
231
231
  :locale=>"es",
232
- :user_id=>4,
233
- :activity=>
232
+ :user_id=>4}
233
+ )
234
+ data.second[:activity].should include(
234
235
  {:num_translations=>1,
235
236
  :num_verifications=>1,
236
237
  :word_count_translations=>13,
237
238
  :word_count_verifications=>8,
238
- :translated_keys=>[["example.key.4", "bef8ae7ec7a9d0145615a1dd8e902e43"]],
239
- :verified_keys=>[["example.key.4", "38a72c02b5febe4975fdcf19d32e412d"]]}}
239
+ :translated_keys=>[["new regime payments key 1", "bef8ae7ec7a9d0145615a1dd8e902e43"]],
240
+ :verified_keys=>[["new regime payments key 1", "38a72c02b5febe4975fdcf19d32e412d"]]}
240
241
  )
241
242
  end
242
243
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: air18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.25
4
+ version: 0.1.26
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2012-11-06 00:00:00.000000000 Z
16
+ date: 2012-11-07 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: i18n