module Clausewitz::Localisation
Constants
- LANG_MAP
- VALID_LANG_REGEX
Public Class Methods
parse(text)
click to toggle source
# File lib/clausewitz/localisation.rb, line 80 def self.parse(text) self.pre_validate(text) contents = {} current_lang = nil text.lines.each do |line| line.strip! next if line.empty? || line =~ /^#/ lang_match = LANG_MAP.keys.find { |lang| line == "#{lang}:" } if lang_match current_lang = lang_match next end unless current_lang errors = ["Cannot parse loc key without a langauge being set!"] fail(UnparseableFileError, errors) end key, value = line.split(/\s/, 2) key.gsub!(/:(\d+)?$/, '') value.gsub!(/(^"|"$)/, '') contents[current_lang] ||= {} contents[current_lang][key] = value end return contents end
parse_file(filepath)
click to toggle source
# File lib/clausewitz/localisation.rb, line 121 def self.parse_file(filepath) contents = nil File.open(filepath, 'r:bom|UTF-8') do |f| contents = f.read end self.parse(contents) end
pre_validate(text)
click to toggle source
# File lib/clausewitz/localisation.rb, line 105 def self.pre_validate(text) errors = [] text.lines[1..-1].each_with_index do |line, index| lineno = index + 2 if line =~ /^\s*([\w\d.'-])+\:([0-9]+)?\"/ errors << "Line ##{lineno} is missing a space between the key and value." end if line.count('"').odd? errors << "Unmatched double quote on line ##{lineno}." end end fail(UnparseableFileError, errors) unless errors.empty? end
valid_lang?(lang)
click to toggle source
# File lib/clausewitz/localisation.rb, line 136 def self.valid_lang?(lang) lang =~ VALID_LANG_REGEX end
validate_localisation!(contents)
click to toggle source
# File lib/clausewitz/localisation.rb, line 129 def self.validate_localisation!(contents) fail("Unknown language keys!") unless contents.keys.all? do |lang| self.valid_lang?(lang) end end