Sha256: 7a289ebec56db61fbe9b94f347560237daebe0f81556160100c36cfb33e06a53
Contents?: true
Size: 1.24 KB
Versions: 14
Compression:
Stored size: 1.24 KB
Contents
require "fileutils" class AwesomeTranslations::TranslatedValue attr_accessor :file, :locale, :key, :value def initialize(data) @file, @locale, @key, @value = data[:file], data[:locale], data[:key], data[:value] end def to_s "<AwesomeTranslations::TranslatedValue file=\"#{@file}\" locale=\"#{@locale}\" key=\"#{@key}\" value=\"#{@value}\">" end alias inspect to_s def save! dir = File.dirname(@file) FileUtils.mkdir_p(dir) unless File.exists?(dir) File.open(@file, "w") { |fp| fp.write("#{@locale}:\n") } unless File.exists?(@file) translations = YAML.load(File.read(@file)) translations ||= {} translations[@locale.to_s] ||= {} current = translations[@locale.to_s] key_parts = key.split(".") last_index = key_parts.length - 1 key_parts.each_with_index do |key_part, index| key_part = key_part.to_s if index == last_index if @value.empty? current.delete(key_part) else current[key_part] = value end else current[key_part] ||= {} current = current[key_part] end end I18n.load_path << file unless I18n.load_path.include?(file) File.open(file, "w") { |fp| fp.write(YAML.dump(translations)) } end end
Version data entries
14 entries across 14 versions & 1 rubygems