Module: SimplesIdeias::I18n
- Extended by:
- I18n
- Included in:
- I18n
- Defined in:
- lib/i18n-js.rb,
lib/i18n-js/railtie.rb,
lib/i18n-js/version.rb
Defined Under Namespace
Modules: Version Classes: Railtie
Constant Summary collapse
- MERGER =
deep_merge by Stefan Rusterholz, see www.ruby-forum.com/topic/142809
proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &MERGER) : v2 }
Instance Method Summary collapse
-
#config ⇒ Object
Load configuration file for partial exporting and custom output directory.
-
#config? ⇒ Boolean
Check if configuration file exist.
- #config_file ⇒ Object
-
#deep_merge(target, hash) ⇒ Object
:nodoc:.
-
#deep_merge!(target, hash) ⇒ Object
:nodoc:.
-
#export! ⇒ Object
Export translations to JavaScript, considering settings from configuration file.
-
#filter(translations, scopes) ⇒ Object
Filter translations according to the specified scope.
- #javascript_file ⇒ Object
-
#save(translations, file) ⇒ Object
Convert translations to JSON string and save file.
-
#scoped_translations(scopes) ⇒ Object
:nodoc:.
-
#setup! ⇒ Object
Copy configuration and JavaScript library files to
config/i18n-js.yml
andpublic/javascripts/i18n.js
. -
#translations ⇒ Object
Initialize and return translations.
-
#update! ⇒ Object
Retrieve an updated JavaScript library from Github.
Instance Method Details
#config ⇒ Object
Load configuration file for partial exporting and custom output directory
41 42 43 |
# File 'lib/i18n-js.rb', line 41 def config HashWithIndifferentAccess.new YAML.load_file(config_file) end |
#config? ⇒ Boolean
Check if configuration file exist
46 47 48 |
# File 'lib/i18n-js.rb', line 46 def config? File.file? config_file end |
#config_file ⇒ Object
12 13 14 |
# File 'lib/i18n-js.rb', line 12 def config_file Rails.root.join("config/i18n-js.yml") end |
#deep_merge(target, hash) ⇒ Object
:nodoc:
114 115 116 |
# File 'lib/i18n-js.rb', line 114 def deep_merge(target, hash) # :nodoc: target.merge(hash, &MERGER) end |
#deep_merge!(target, hash) ⇒ Object
:nodoc:
118 119 120 |
# File 'lib/i18n-js.rb', line 118 def deep_merge!(target, hash) # :nodoc: target.merge!(hash, &MERGER) end |
#export! ⇒ Object
Export translations to JavaScript, considering settings from configuration file
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/i18n-js.rb', line 22 def export! if config? for in config[:translations] .reverse_merge!(:only => "*") if [:only] == "*" save translations, [:file] else result = scoped_translations([:only]) save result, [:file] unless result.empty? end end else save translations, "public/javascripts/translations.js" end end |
#filter(translations, scopes) ⇒ Object
Filter translations according to the specified scope.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/i18n-js.rb', line 88 def filter(translations, scopes) scopes = scopes.split(".") if scopes.is_a?(String) scopes = scopes.clone scope = scopes.shift if scope == "*" results = {} translations.each do |scope, translations| tmp = scopes.empty? ? translations : filter(translations, scopes) results[scope.to_sym] = tmp unless tmp.nil? end return results elsif translations.has_key?(scope.to_sym) return {scope.to_sym => scopes.empty? ? translations[scope.to_sym] : filter(translations[scope.to_sym], scopes)} end nil end |
#javascript_file ⇒ Object
16 17 18 |
# File 'lib/i18n-js.rb', line 16 def javascript_file Rails.root.join("public/javascripts/i18n.js") end |
#save(translations, file) ⇒ Object
Convert translations to JSON string and save file.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/i18n-js.rb', line 65 def save(translations, file) file = Rails.root.join(file) FileUtils.mkdir_p File.dirname(file) File.open(file, "w+") do |f| f << %(var I18n = I18n || {};\n) f << %(I18n.translations = ); f << translations.to_json f << %(;) end end |
#scoped_translations(scopes) ⇒ Object
:nodoc:
77 78 79 80 81 82 83 84 85 |
# File 'lib/i18n-js.rb', line 77 def scoped_translations(scopes) # :nodoc: result = {} [scopes].flatten.each do |scope| deep_merge! result, filter(translations, scope) end result end |
#setup! ⇒ Object
Copy configuration and JavaScript library files to config/i18n-js.yml
and public/javascripts/i18n.js
.
52 53 54 55 |
# File 'lib/i18n-js.rb', line 52 def setup! FileUtils.cp File.dirname(__FILE__) + "/../source/i18n.js", javascript_file FileUtils.cp(File.dirname(__FILE__) + "/../source/i18n-js.yml", config_file) unless config? end |
#translations ⇒ Object
Initialize and return translations
107 108 109 110 111 112 |
# File 'lib/i18n-js.rb', line 107 def translations ::I18n.backend.instance_eval do init_translations unless initialized? translations end end |
#update! ⇒ Object
Retrieve an updated JavaScript library from Github.
58 59 60 61 62 |
# File 'lib/i18n-js.rb', line 58 def update! require "open-uri" contents = open("http://github.com/fnando/i18n-js/raw/master/lib/i18n.js").read File.open(javascript_file, "w+") {|f| f << contents} end |