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
-
#assert_usable_configuration! ⇒ Object
Under rails 3.1.1 and higher, perform a check to ensure that the full environment will be available during asset compilation.
-
#config ⇒ Object
Load configuration file for partial exporting and custom output directory.
-
#config? ⇒ Boolean
Check if configuration file exist.
- #config_file ⇒ Object
- #configured_segments ⇒ 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.
- #has_asset_pipeline? ⇒ Boolean
- #javascript_file ⇒ Object
-
#save(translations, file) ⇒ Object
Convert translations to JSON string and save file.
-
#scoped_translations(scopes) ⇒ Object
:nodoc:.
- #segment_for_scope(scope) ⇒ Object
- #segments_per_locale(pattern, scope) ⇒ Object
-
#setup! ⇒ Object
Copy configuration and JavaScript library files to
config/i18n-js.yml
andpublic/javascripts/i18n.js
. - #translation_segments ⇒ Object
-
#translations ⇒ Object
Initialize and return translations.
-
#update! ⇒ Object
Retrieve an updated JavaScript library from Github.
Instance Method Details
#assert_usable_configuration! ⇒ Object
Under rails 3.1.1 and higher, perform a check to ensure that the full environment will be available during asset compilation. This is required to ensure I18n is loaded.
17 18 19 20 21 |
# File 'lib/i18n-js.rb', line 17 def assert_usable_configuration! @usable_configuration ||= Rails.version >= "3.1.1" && Rails.configuration.assets.initialize_on_precompile || raise("Cannot precompile i18n-js translations unless environment is initialized. Please set config.assets.initialize_on_precompile to true.") end |
#config ⇒ Object
Load configuration file for partial exporting and custom output directory
93 94 95 96 97 98 99 |
# File 'lib/i18n-js.rb', line 93 def config if config? (YAML.load_file(config_file) || {}).with_indifferent_access else {} end end |
#config? ⇒ Boolean
Check if configuration file exist
102 103 104 |
# File 'lib/i18n-js.rb', line 102 def config? File.file? config_file end |
#config_file ⇒ Object
27 28 29 |
# File 'lib/i18n-js.rb', line 27 def config_file Rails.root.join("config/i18n-js.yml") end |
#configured_segments ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/i18n-js.rb', line 71 def configured_segments config[:translations].each_with_object({}) do |,segments| .reverse_merge!(:only => "*") if [:file] =~ ::I18n::INTERPOLATION_PATTERN segments.merge!(segments_per_locale([:file],[:only])) else result = segment_for_scope([:only]) segments[[:file]] = result unless result.empty? end end end |
#deep_merge(target, hash) ⇒ Object
:nodoc:
169 170 171 |
# File 'lib/i18n-js.rb', line 169 def deep_merge(target, hash) # :nodoc: target.merge(hash || { }, &MERGER) end |
#deep_merge!(target, hash) ⇒ Object
:nodoc:
173 174 175 |
# File 'lib/i18n-js.rb', line 173 def deep_merge!(target, hash) # :nodoc: target.merge!(hash || { }, &MERGER) end |
#export! ⇒ Object
Export translations to JavaScript, considering settings from configuration file
45 46 47 48 49 50 |
# File 'lib/i18n-js.rb', line 45 def export! translation_segments.map do |filename, translations| save(translations, filename) translations end.inject({ }){ |a,b| a.merge(b)} end |
#export_dir ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/i18n-js.rb', line 31 def export_dir if has_asset_pipeline? "app/assets/javascripts/i18n" else "public/javascripts" end end |
#filter(translations, scopes) ⇒ Object
Filter translations according to the specified scope.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/i18n-js.rb', line 143 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.is_a?(Hash) && 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 |
#has_asset_pipeline? ⇒ Boolean
23 24 25 |
# File 'lib/i18n-js.rb', line 23 def has_asset_pipeline? Rails.configuration.respond_to?(:assets) && Rails.configuration.assets.enabled end |
#javascript_file ⇒ Object
39 40 41 |
# File 'lib/i18n-js.rb', line 39 def javascript_file Rails.root.join(export_dir, "i18n.js") end |
#save(translations, file) ⇒ Object
Convert translations to JSON string and save file.
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/i18n-js.rb', line 121 def save(translations, file) file = Rails.root.join(file) FileUtils.mkdir_p File.dirname(file) File.open(file, "wb+") do |f| f << %(var I18n = I18n || {};\n) f << %(I18n.translations = ); f << translations.to_json f << %(;) end end |
#scoped_translations(scopes) ⇒ Object
:nodoc:
133 134 135 136 137 138 139 140 |
# File 'lib/i18n-js.rb', line 133 def scoped_translations(scopes) # :nodoc: result = {} [scopes].flatten.each do |scope| deep_merge! result, filter(translations, scope) end result end |
#segment_for_scope(scope) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/i18n-js.rb', line 63 def segment_for_scope(scope) if scope == "*" translations else scoped_translations(scope) end end |
#segments_per_locale(pattern, scope) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/i18n-js.rb', line 52 def segments_per_locale(pattern,scope) ::I18n.available_locales.each_with_object({}) do |locale,segments| scopes = [scope].flatten.map{ |s| "#{locale}.#{s}"} result = scoped_translations(scopes) unless result.empty? segment_name = ::I18n.interpolate(pattern,{:locale => locale}) segments[segment_name] = result end end end |
#setup! ⇒ Object
Copy configuration and JavaScript library files to config/i18n-js.yml
and public/javascripts/i18n.js
.
108 109 110 111 |
# File 'lib/i18n-js.rb', line 108 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 |
#translation_segments ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/i18n-js.rb', line 83 def translation_segments if config? && config[:translations] configured_segments else {"#{export_dir}/translations.js" => translations} end end |
#translations ⇒ Object
Initialize and return translations
162 163 164 165 166 167 |
# File 'lib/i18n-js.rb', line 162 def translations ::I18n.backend.instance_eval do init_translations unless initialized? translations end end |
#update! ⇒ Object
Retrieve an updated JavaScript library from Github.
114 115 116 117 118 |
# File 'lib/i18n-js.rb', line 114 def update! require "open-uri" contents = open("https://raw.github.com/fnando/i18n-js/master/vendor/assets/javascripts/i18n.js").read File.open(javascript_file, "w+") {|f| f << contents} end |