Class: Rails::Pseudoloc::Codec

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/pseudoloc/codec.rb

Overview

Encodes and decodes strings from the pseudolocalized character set.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table = Codec.default_table) ⇒ Codec

Constructs a codec for the given translation table.

Parameters:

  • table (Hash) (defaults to: Codec.default_table)

    Mappings from input characters to arrays of possible output characters.



46
47
48
# File 'lib/rails/pseudoloc/codec.rb', line 46

def initialize(table = Codec.default_table)
  @table = table
end

Class Method Details

.default_tableObject

Returns Default pseudolocalization table.

Returns:

  • Default pseudolocalization table.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rails/pseudoloc/codec.rb', line 12

def self.default_table
  {
    'a' => 'àáâãäåāăąǻάαад',
    'b' => 'þьБъ',
    'c' => '¢çćĉċčсς',
    'd' => 'ďđ',
    'e' => 'èéêëēĕėęěέεеёє℮',
    'f' => 'ƒ',
    'g' => 'ĝğġģ',
    'h' => 'ĥħћђ',
    'i' => 'ìíîïĩīĭįίιϊіїΐ',
    'j' => 'ĵј',
    'k' => 'ķ',
    'l' => 'ĺļľŀłℓ',
    'm' => 'm',
    'n' => 'ήηńņňʼnŋñ',
    'o' => 'òóôõöøōŏőοσόо',
    'p' => 'þρр',
    'r' => 'ŕŗřѓґгř',
    's' => 'śŝşѕš',
    't' => 'ţť',
    'u' => 'µùúûüũūŭůűųΰυϋύ',
    'w' => 'ŵωώẁẃẅ',
    'x' => 'хж',
    'y' => 'ýÿŷўỳу',
    'z' => 'źżž'
  }
end

Instance Method Details

#encode(text) ⇒ String

Encodes the given text using the translation table.

Parameters:

  • text (String)

    Text to encode.

Returns:

  • (String)

    Encoded text.



54
55
56
57
58
59
60
61
62
63
# File 'lib/rails/pseudoloc/codec.rb', line 54

def encode(text)
  output = ''

  text.each_char.each_with_index do |char, i|
    alts = @table.fetch(char, [char])
    output << alts[i % alts.length]
  end

  output
end