lib/lolita-i18n/backend.rb in lolita-i18n-0.3.0 vs lib/lolita-i18n/backend.rb in lolita-i18n-0.3.1
- old
+ new
@@ -1,7 +1,7 @@
module Lolita
- module I18n
+ module I18n
# Allow to operate with I18n keys and values.
class Backend
class << self
# Get translation and decode it. Set translate from and to translations.
@@ -22,10 +22,11 @@
# Store translation, decode and store.
# Accept:
# * <tt>key</tt> - translation key
# * <tt>translation</tt> - String with translation
def set(key,translation)
+ validate_translation(key,translation)
locale=translate_to(key)
translation_key=translation_key(key)
value=Yajl::Parser.parse(translation.to_json)
if value.blank?
del key
@@ -49,9 +50,19 @@
def translation_key(key)
(key.to_s.split('.')[1..-1]).join(".")
end
private
+
+ def validate_translation key,translation
+ validate_interpolation_arguments key,translation
+ end
+
+ def validate_interpolation_arguments key,translation
+ in_translation = translation.scan(/(%{\w+})/).map{|m| m.first}.sort
+ in_original = ::I18n.t(translation_key(key), :locale => ::I18n.default_locale).scan(/(%{\w+})/).map{|m| m.first}.sort
+ raise Exceptions::MissingInterpolationArgument.new(in_original) unless in_translation == in_original
+ end
def keys
@keys||=Lolita.i18n.flattened_translations.keys.sort
end
\ No newline at end of file