lib/i18n_screwdriver.rb in i18n_screwdriver-6.0 vs lib/i18n_screwdriver.rb in i18n_screwdriver-7.0

- old
+ new

@@ -6,11 +6,11 @@ module I18nScrewdriver def self.filename_for_locale(locale) File.join("config", "locales", "application.#{locale}.yml") end - def self.for_key(string) + def self.generate_key(string) string = string.strip (string =~ /^:[a-z][a-z0-9_]*$/) ? string : Digest::MD5.hexdigest(string) end def self.file_with_translations_exists?(locale) @@ -88,12 +88,12 @@ Dir.glob("**/*.{haml,erb,slim,rb}").each do |file| input = File.read(file) texts.concat(grab_texts_to_be_translated(input)) symbols.concat(grab_symbols_to_be_translated(input)) end - translations = Hash[texts.uniq.map{ |text| [for_key(text), text] }] - translations.merge(Hash[symbols.uniq.map{ |symbol| [for_key(symbol), ""] }]) + translations = Hash[texts.uniq.map{ |text| [generate_key(text), extract_text(text)] }] + translations.merge(Hash[symbols.uniq.map{ |symbol| [generate_key(symbol), ""] }]) end def self.default_locale @default_locale ||= begin raise "Please set I18.default_locale" unless I18n.default_locale.present? @@ -126,7 +126,16 @@ def self.sanitize_hash(hash) {}.tap do |new_hash| hash.each{ |k, v| new_hash[k.to_s] = v.to_s } end + end + + def self.translate(string, options = {}) + I18n.translate(generate_key(string), options) + end + + def self.extract_text(string) + namespace, text = string.split("|", 2) + text ? text : namespace end end