air18n 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -284,7 +284,7 @@ module Air18n
284
284
  result = result.strip if result.present?
285
285
 
286
286
  # Handle pseudo-locales.
287
- result = PseudoLocales.translate(locale, result)
287
+ result = PseudoLocales.translate(locale, key, result)
288
288
 
289
289
  # Handle smart counts.
290
290
  result = SmartCount.choose(result, locale_fallen_back_to, options[SmartCount::INTERPOLATION_VARIABLE_NAME])
@@ -396,5 +396,15 @@ module Air18n
396
396
  def has_translation?(key, locale)
397
397
  @air18n_backend.has_translation?(key, locale)
398
398
  end
399
+
400
+ # Return the air18n backend's available locales. Advantages/disadvantages over asking the Rails backend:
401
+ # Advantage: returns only locales that we actually have translations for.
402
+ # Disadvantage: when all locales haven't been sucked in from DB, won't
403
+ # return all locales we support.
404
+ # Disadvantage: auto-generated locales like Singaporean English won't be
405
+ # included.
406
+ def available_locales
407
+ @air18n_backend.available_locales
408
+ end
399
409
  end
400
410
  end
@@ -2,12 +2,14 @@ module Air18n
2
2
  module PseudoLocales
3
3
  module_function
4
4
 
5
- def translate(locale, text)
5
+ def translate(locale, key, text)
6
6
  case locale
7
7
  when :xxlong
8
8
  elongate(text)
9
9
  when :xx
10
10
  xout(text)
11
+ when :xy
12
+ mix(key, text)
11
13
  else
12
14
  text
13
15
  end
@@ -49,5 +51,19 @@ module Air18n
49
51
 
50
52
  words.join(' ')
51
53
  end
54
+
55
+ def mix(key, text)
56
+ max_byte_size = text.bytesize
57
+ longest_translation = text
58
+ I18n.available_locales.each do |l|
59
+ translation = I18n.t(key, :locale => l)
60
+ translation_bytesize = translation.bytesize
61
+ if translation_bytesize > max_byte_size && !translation.starts_with?('translation missing')
62
+ max_byte_size = translation_bytesize
63
+ longest_translation = translation
64
+ end
65
+ end
66
+ longest_translation
67
+ end
52
68
  end
53
69
  end
@@ -1,3 +1,3 @@
1
1
  module Air18n
2
- VERSION = "0.4.8"
2
+ VERSION = "0.4.9"
3
3
  end
@@ -173,7 +173,7 @@ describe Air18n::Backend do
173
173
  context 'Pseudolocales' do
174
174
  it 'should use pseudolocales for xx locale' do
175
175
  @backend = Air18n::Backend.new
176
- @backend.lookup(:xx, 'xx, key 1', @scope, :default => 'hey there').should == Air18n::PseudoLocales::translate(:xx, 'hey there')
176
+ @backend.lookup(:xx, 'xx, key 1', @scope, :default => 'hey there').should == Air18n::PseudoLocales::translate(:xx, 'xx, key1', 'hey there')
177
177
  end
178
178
  end
179
179
 
@@ -1,17 +1,31 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Air18n::PseudoLocales do
4
6
  context 'xx' do
5
7
  it 'should work' do
6
- Air18n::PseudoLocales.translate(:xx, 'here be %{num_pirates}, pirates.').should == '[xxxx xx %{num_pirates}, xxxxxxx.]'
8
+ Air18n::PseudoLocales.translate(:xx, 'pirates', 'here be %{num_pirates}, pirates.').should == '[xxxx xx %{num_pirates}, xxxxxxx.]'
7
9
  end
8
10
  end
9
11
 
10
- context 'xx-long' do
12
+ context 'xxlong' do
11
13
  it 'should work' do
12
- Air18n::PseudoLocales.translate(:xxlong, 'here be %{num_pirates}, pirates.').should == 'here be %{num_pirates}, pirates. one two three four five six seven'
13
- Air18n::PseudoLocales.translate(:xxlong, 'hey there').should == 'hey there one two'
14
- Air18n::PseudoLocales.translate(:xxlong, 'h').should == 'h one'
14
+ Air18n::PseudoLocales.translate(:xxlong, 'pirates', 'here be %{num_pirates}, pirates.').should == 'here be %{num_pirates}, pirates. one two three four five six seven'
15
+ Air18n::PseudoLocales.translate(:xxlong, 'oh hey', 'hey there').should == 'hey there one two'
16
+ Air18n::PseudoLocales.translate(:xxlong, 'khi', 'h').should == 'h one'
17
+ end
18
+ end
19
+
20
+ context 'xy' do
21
+ it 'should choose longest translation' do
22
+ I18n.backend.store_translations(:fi, { 'blackberry' => 'karhunvatukka' })
23
+ I18n.backend.store_translations(:zh, { 'blackberry' => '黑莓' })
24
+ Air18n::PseudoLocales.translate(:xy, 'blackberry', 'blackberry').should == 'karhunvatukka'
25
+ end
26
+
27
+ it 'should work for novel keys' do
28
+ Air18n::PseudoLocales.translate(:xy, 'brand spanking new key', 'some special text').should == 'some special text'
15
29
  end
16
30
  end
17
31
  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.4.8
4
+ version: 0.4.9
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: 2013-05-13 00:00:00.000000000 Z
16
+ date: 2013-05-17 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: i18n