Class: CopyTunerClient::I18nBackend

Inherits:
Object
  • Object
show all
Includes:
I18n::Backend::Simple::Implementation
Defined in:
lib/copy_tuner_client/i18n_backend.rb

Overview

I18n implementation designed to synchronize with CopyTuner.

Expects an object that acts like a Hash, responding to [], []=, and keys.

This backend will be used as the default I18n backend when the client is configured, so you will not need to instantiate this class from the application. Instead, just use methods on the I18n class.

This implementation will also load translations from locale files.

Instance Method Summary collapse

Constructor Details

#initialize(cache) ⇒ I18nBackend

Usually instantiated when Configuration#apply is invoked.

Parameters:

  • cache (Cache)

    must act like a hash, returning and accept blurbs by key.



20
21
22
23
24
# File 'lib/copy_tuner_client/i18n_backend.rb', line 20

def initialize(cache)
  @cache = cache
  @tree_cache = nil
  @cache_version = nil
end

Instance Method Details

#available_localesArray<String>

Returns locales available for this CopyTuner project.

Returns:

  • (Array<String>)

    available locales



44
45
46
47
48
# File 'lib/copy_tuner_client/i18n_backend.rb', line 44

def available_locales
  return @available_locales if defined?(@available_locales)
  cached_locales = cache.keys.map { |key| key.split('.').first }
  @available_locales = (cached_locales + super).uniq.map { |locale| locale.to_sym }
end

#store_translations(locale, data, options = {}) ⇒ Object

Stores the given translations.

Updates will be visible in the current process immediately, and will propagate to CopyTuner during the next flush.

Parameters:

  • locale (String)

    the locale (ie “en”) to store translations for

  • data (Hash)

    nested key-value pairs to be added as blurbs

  • options (Hash) (defaults to: {})

    unused part of the I18n API



58
59
60
61
# File 'lib/copy_tuner_client/i18n_backend.rb', line 58

def store_translations(locale, data, options = {})
  super
  store_item(locale, data)
end

#translate(locale, key, options = {}) ⇒ Object

Returns the translated key (usually a String).

Returns:

  • (Object)

    the translated key (usually a String)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/copy_tuner_client/i18n_backend.rb', line 27

def translate(locale, key, options = {})
  # I18nの標準処理に任せる(内部でlookupが呼ばれる)
  content = super(locale, key, options)

  return content if content.nil? || content.is_a?(Hash)

  # HTML escapeの処理(ツリー構造のHashは除く)
  if CopyTunerClient.configuration.html_escape
    content
  else
    # Backward compatible
    content.respond_to?(:html_safe) ? content.html_safe : content
  end
end