class Rails::Pseudoloc::Codec

Encodes and decodes strings from the pseudolocalized character set.

Public Class Methods

default_table() click to toggle source

@return Default pseudolocalization table.

# 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
new(table = Codec.default_table) click to toggle source

Constructs a codec for the given translation table.

@param [Hash] table Mappings from input characters to arrays of possible output characters.

# File lib/rails/pseudoloc/codec.rb, line 46
def initialize(table = Codec.default_table)
  @table = table
end

Public Instance Methods

encode(text) click to toggle source

Encodes the given text using the translation table.

@param [String] text Text to encode. @return [String] Encoded text.

# 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