Sha256: b704763fa6408c7fe1ea1d4261aba4b96a211fa6bff29f91f9abb75f823f4c15

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 KB

Contents

require 'yaml'

module Clausewitz
  module Localisation
    LANG_MAP = {
      'english' => {
        base: 'en'
      },
      'french' => {
        base: 'fr'
      },
      'german' => {
        base: 'de'
      },
      'portuguese' => {
        base: 'pt'
      },
      'russian' => {
        base: 'ru'
      },
      'spanish' => {
        base: 'es'
      }
    }

    def self.parse(text)
      smudged_text = text.lines.map do |line|
        self.smudge_key(line)
      end
      contents = YAML.load(smudged_text.join("\n"))
      self.validate_localisation!(contents)
      contents.each do |lang, entries|
        entry_keys = entries ? entries.keys : []
        entry_keys.each do |key|
          entries[unsmudge_key(key)] = entries.delete(key)
        end
      end
      contents
    end

    def self.parse_file(filepath)
      contents = nil
      File.open(filepath, 'r:UTF-8') do |f|
        contents = f.read
      end
      self.parse(contents)
    end

    def self.smudge_key(line)
      line.sub(/\:([0-9]+) /, '!!!\1: ')
    end

    def self.validate_localisation!(contents)
      fail("Unknown language keys!") unless contents.keys.all? do |lang|
        self.valid_lang?(lang)
      end
    end

    VALID_LANG_REGEX = /^l_(#{LANG_MAP.keys.join('|')})/
    def self.valid_lang?(lang)
      lang =~ VALID_LANG_REGEX
    end

    def self.unsmudge_key(line)
      line.sub(/!!!([0-9]+)$/, ':\1')
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
clausewitz-spelling-0.1.17 lib/clausewitz/localisation.rb
clausewitz-spelling-0.1.16 lib/clausewitz/localisation.rb
clausewitz-spelling-0.1.15 lib/clausewitz/localisation.rb
clausewitz-spelling-0.1.14 lib/clausewitz/localisation.rb
clausewitz-spelling-0.1.13 lib/clausewitz/localisation.rb
clausewitz-spelling-0.1.12 lib/clausewitz/localisation.rb
clausewitz-spelling-0.1.11 lib/clausewitz/localisation.rb