Module: Air18n::PseudoLocales

Defined in:
lib/air18n/pseudo_locales.rb

Constant Summary collapse

DEFAULT_NUM_WORDS_THRESHOLD =

elongate() ported from Google’s Pseudolocalization Tool library for Java. code.google.com/p/pseudolocalization-tool/

3
NUMBERS =
[ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen",  "nineteen", "twenty", "twentyone", "twentytwo",
"twentythree", "twentyfour", "twentyfive", "twentysix", "twentyseven",
"twentyeight", "twentynine", "thirty", "thirtyone", "thirtytwo",
"thirtythree", "thirtyfour", "thirtyfive", "thirtysix", "thirtyseven",
"thirtyeight", "thirtynine", "forty" ]

Class Method Summary collapse

Class Method Details

.elongate(text) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/air18n/pseudo_locales.rb', line 35

def elongate(text)
  words = text.split
  expansion = text.size
  if words.size <= DEFAULT_NUM_WORDS_THRESHOLD
    # For short strings, expand by 50% but at least 1 character.
    expansion = (expansion + 1) / 2;
  end

  expandedText = text
  wordIndex = 0
  while (expansion > 0)
    word = NUMBERS[wordIndex % NUMBERS.size]
    words << word
    expansion -= word.length() + 1;
    wordIndex += 1
  end

  words.join(' ')
end

.mix(key, text) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/air18n/pseudo_locales.rb', line 55

def mix(key, text)
  max_byte_size = text.bytesize
  longest_translation = text
  I18n.available_locales.each do |l|
    translation = I18n.t(key, :locale => l)
    translation_bytesize = translation.bytesize
    if translation_bytesize > max_byte_size && !translation.starts_with?('translation missing')
      max_byte_size = translation_bytesize
      longest_translation = translation
    end
  end
  longest_translation
end

.translate(locale, key, text) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/air18n/pseudo_locales.rb', line 5

def translate(locale, key, text)
  case locale
  when :xxlong
    elongate(text)
  when :xx
    xout(text)
  when :xy
    mix(key, text)
  else
    text
  end
end

.xout(text) ⇒ Object



18
19
20
# File 'lib/air18n/pseudo_locales.rb', line 18

def xout(text)
  '[' +  text.split.map { |word| (word =~ /%\{.+\}/) ? word : word.gsub(/\w/, 'x') }.join(' ') + ']'
end