Module: SimplesIdeias::I18n
- Extended by:
- I18n
- Included in:
- I18n
- Defined in:
- lib/i18n-js.rb,
lib/i18n-js/engine.rb,
lib/i18n-js/railtie.rb,
lib/i18n-js/version.rb,
lib/i18n-js/middleware.rb
Defined Under Namespace
Modules: Version Classes: Engine, Middleware, 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.
- #export_dir ⇒ Object
-
#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
51 52 53 54 55 56 57 |
# File 'lib/i18n-js.rb', line 51 def config if config? (YAML.load_file(config_file) || {}).with_indifferent_access else {} end end |
#config? ⇒ Boolean
Check if configuration file exist
60 61 62 |
# File 'lib/i18n-js.rb', line 60 def config? File.file? config_file end |
#config_file ⇒ Object
14 15 16 |
# File 'lib/i18n-js.rb', line 14 def config_file Rails.root.join("config/i18n-js.yml") end |
#deep_merge(target, hash) ⇒ Object
:nodoc:
128 129 130 |
# File 'lib/i18n-js.rb', line 128 def deep_merge(target, hash) # :nodoc: target.merge(hash, &MERGER) end |
#deep_merge!(target, hash) ⇒ Object
:nodoc:
132 133 134 |
# File 'lib/i18n-js.rb', line 132 def deep_merge!(target, hash) # :nodoc: target.merge!(hash, &MERGER) end |
#export! ⇒ Object
Export translations to JavaScript, considering settings from configuration file
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/i18n-js.rb', line 32 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, "#{export_dir}/translations.js" end end |
#export_dir ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/i18n-js.rb', line 18 def export_dir if Rails.version >= "3.1" && Rails.configuration.assets.enabled "app/assets/javascripts/i18n" else "public/javascripts" end end |
#filter(translations, scopes) ⇒ Object
Filter translations according to the specified scope.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/i18n-js.rb', line 102 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
26 27 28 |
# File 'lib/i18n-js.rb', line 26 def javascript_file Rails.root.join(export_dir, "i18n.js") end |
#save(translations, file) ⇒ Object
Convert translations to JSON string and save file.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/i18n-js.rb', line 79 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:
91 92 93 94 95 96 97 98 99 |
# File 'lib/i18n-js.rb', line 91 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
.
66 67 68 69 |
# File 'lib/i18n-js.rb', line 66 def setup! FileUtils.cp(File.dirname(__FILE__) + "/../vendor/assets/javascripts/i18n.js", javascript_file) unless Rails.version >= "3.1" FileUtils.cp(File.dirname(__FILE__) + "/../config/i18n-js.yml", config_file) unless config? end |
#translations ⇒ Object
Initialize and return translations
121 122 123 124 125 126 |
# File 'lib/i18n-js.rb', line 121 def translations ::I18n.backend.instance_eval do init_translations unless initialized? translations end end |
#update! ⇒ Object
Retrieve an updated JavaScript library from Github.
72 73 74 75 76 |
# File 'lib/i18n-js.rb', line 72 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 |