module I18n::JS
Constants
- DEFAULT_CONFIG_PATH
- DEFAULT_EXPORT_DIR_PATH
- VERSION
Public Class Methods
backend()
click to toggle source
Allow using a different backend than the one globally configured
# File lib/i18n/js.rb, line 35 def self.backend @backend ||= I18n.backend end
backend=(alternative_backend)
click to toggle source
# File lib/i18n/js.rb, line 39 def self.backend=(alternative_backend) @backend = alternative_backend end
config()
click to toggle source
Load configuration file for partial exporting and custom output directory
# File lib/i18n/js.rb, line 113 def self.config Private::ConfigStore.instance.fetch do if config_file_exists? erb_result_from_yaml_file = ERB.new(File.read(config_file_path)).result Private::HashWithSymbolKeys.new( (::YAML.load(erb_result_from_yaml_file) || {}) ) else Private::HashWithSymbolKeys.new({}) end.freeze end end
config_file_exists?()
click to toggle source
@api private Check if configuration file exist
# File lib/i18n/js.rb, line 128 def self.config_file_exists? File.file? config_file_path end
config_file_path()
click to toggle source
The configuration file. This defaults to the `config/i18n-js.yml` file.
# File lib/i18n/js.rb, line 24 def self.config_file_path @config_file_path ||= DEFAULT_CONFIG_PATH end
config_file_path=(new_path)
click to toggle source
# File lib/i18n/js.rb, line 28 def self.config_file_path=(new_path) @config_file_path = new_path # new config file path = need to re-read config from new file Private::ConfigStore.instance.flush_cache end
configured_segments()
click to toggle source
# File lib/i18n/js.rb, line 59 def self.configured_segments config[:translations].inject([]) do |segments, options_hash| options_hash_with_symbol_keys = Private::HashWithSymbolKeys.new(options_hash) file = options_hash_with_symbol_keys[:file] only = options_hash_with_symbol_keys[:only] || '*' exceptions = [options_hash_with_symbol_keys[:except] || []].flatten result = segment_for_scope(only, exceptions) merge_with_fallbacks!(result) if fallbacks unless result.empty? segments << Segment.new( file, result, extract_segment_options(options_hash_with_symbol_keys), ) end segments end end
exclude(translations, exceptions)
click to toggle source
Exclude keys from translations listed in the `except:` section in the config file
# File lib/i18n/js.rb, line 146 def self.exclude(translations, exceptions) return translations if exceptions.empty? exceptions.inject(translations) do |memo, exception| exception_scopes = exception.to_s.split(".") Utils.deep_reject(memo) do |key, value, scopes| Utils.scopes_match?(scopes, exception_scopes) end end end
export()
click to toggle source
Export translations to JavaScript, considering settings from configuration file
# File lib/i18n/js.rb, line 45 def self.export export_i18n_js translation_segments.each(&:save!) end
export_i18n_js()
click to toggle source
Copy i18n.js
# File lib/i18n/js.rb, line 249 def self.export_i18n_js return unless export_i18n_js_dir_path.is_a? String FileUtils.mkdir_p(export_i18n_js_dir_path) i18n_js_path = File.expand_path('../../../app/assets/javascripts/i18n.js', __FILE__) destination_path = File.expand_path("i18n.js", export_i18n_js_dir_path) return if File.exist?(destination_path) && FileUtils.identical?(i18n_js_path, destination_path) FileUtils.cp(i18n_js_path, export_i18n_js_dir_path) end
export_i18n_js_dir_path()
click to toggle source
# File lib/i18n/js.rb, line 261 def self.export_i18n_js_dir_path @export_i18n_js_dir_path ||= (config[:export_i18n_js] || :none) if config.key?(:export_i18n_js) @export_i18n_js_dir_path ||= DEFAULT_EXPORT_DIR_PATH @export_i18n_js_dir_path end
export_i18n_js_dir_path=(new_path)
click to toggle source
Setting this to nil would disable i18n.js exporting
# File lib/i18n/js.rb, line 268 def self.export_i18n_js_dir_path=(new_path) new_path = :none unless new_path.is_a? String @export_i18n_js_dir_path = new_path end
extract_segment_options(options)
click to toggle source
# File lib/i18n/js.rb, line 236 def self.extract_segment_options(options) segment_options = Private::HashWithSymbolKeys.new({ js_extend: js_extend, sort_translation_keys: sort_translation_keys?, json_only: json_only }).freeze segment_options.merge(options.slice(*Segment::OPTIONS)) end
fallbacks()
click to toggle source
# File lib/i18n/js.rb, line 202 def self.fallbacks config.fetch(:fallbacks) do # default value true end end
filter(translations, scopes)
click to toggle source
Filter translations according to the specified scope.
# File lib/i18n/js.rb, line 158 def self.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.respond_to?(:key?) && translations.key?(scope.to_sym) return {scope.to_sym => scopes.empty? ? translations[scope.to_sym] : filter(translations[scope.to_sym], scopes)} end nil end
filtered_translations()
click to toggle source
# File lib/i18n/js.rb, line 93 def self.filtered_translations translations = {}.tap do |result| translation_segments.each do |segment| Utils.deep_merge!(result, segment.translations) end end return Utils.deep_key_sort(translations) if I18n::JS.sort_translation_keys? translations end
js_available_locales()
click to toggle source
Get all available locales.
@return [Array<Symbol>] the locales.
# File lib/i18n/js.rb, line 219 def self.js_available_locales config.fetch(:js_available_locales) do # default value I18n.available_locales end.map(&:to_sym) end
js_extend()
click to toggle source
# File lib/i18n/js.rb, line 209 def self.js_extend config.fetch(:js_extend) do # default value true end end
json_only()
click to toggle source
# File lib/i18n/js.rb, line 195 def self.json_only config.fetch(:json_only) do # default value false end end
merge_with_fallbacks!(result)
click to toggle source
deep_merge! given result with result for fallback locale
# File lib/i18n/js.rb, line 83 def self.merge_with_fallbacks!(result) js_available_locales.each do |locale| fallback_locales = FallbackLocales.new(fallbacks, locale) fallback_locales.each do |fallback_locale| # `result[fallback_locale]` could be missing result[locale] = Utils.deep_merge(result[fallback_locale] || {}, result[locale] || {}) end end end
segment_for_scope(scope, exceptions)
click to toggle source
# File lib/i18n/js.rb, line 51 def self.segment_for_scope(scope, exceptions) if scope == "*" exclude(translations, exceptions) else scoped_translations(scope, exceptions) end end
sort_translation_keys=(value)
click to toggle source
# File lib/i18n/js.rb, line 232 def self.sort_translation_keys=(value) @sort_translation_keys = !!value end
sort_translation_keys?()
click to toggle source
# File lib/i18n/js.rb, line 226 def self.sort_translation_keys? @sort_translation_keys ||= (config[:sort_translation_keys]) if config.key?(:sort_translation_keys) @sort_translation_keys = true if @sort_translation_keys.nil? @sort_translation_keys end
translation_segments()
click to toggle source
# File lib/i18n/js.rb, line 103 def self.translation_segments if config_file_exists? && config[:translations] configured_segments else [Segment.new("#{DEFAULT_EXPORT_DIR_PATH}/translations.js", translations)] end end
translations()
click to toggle source
Initialize and return translations
# File lib/i18n/js.rb, line 177 def self.translations self.backend.instance_eval do init_translations unless initialized? # When activesupport is absent, # the core extension (`#slice`) from `i18n` gem will be used instead # And it's causing errors (at least in test) # # So the input is wrapped by our class for better `#slice` Private::HashWithSymbolKeys.new(translations). slice(*::I18n::JS.js_available_locales). to_h end end
use_fallbacks?()
click to toggle source
# File lib/i18n/js.rb, line 191 def self.use_fallbacks? fallbacks != false end