Class: Hanami::Providers::I18n Private

Inherits:
Hanami::Provider::Source show all
Defined in:
lib/hanami/providers/i18n.rb,
lib/hanami/providers/i18n/backend.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

Defined Under Namespace

Classes: Backend

Constant Summary collapse

SLICE_CONFIGURED_SETTINGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

%i[
  default_locale available_locales load_path shared_load_path fallbacks
].freeze
DEFAULT_LOCALE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

:en
DEFAULT_AVAILABLE_LOCALES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

[].freeze
DEFAULT_LOAD_PATH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

["config/i18n/**/*.{yml,yaml,json,rb}"].freeze
DEFAULT_SHARED_LOAD_PATH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

["config/i18n/shared/**/*.{yml,yaml,json,rb}"].freeze
BUNDLED_DEFAULTS_PATH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Built-in English baseline for date/time localization. Loaded into every slice before any user translation files, so users can supply overrides if desired.

Since:

  • 0.1.0

File.expand_path("i18n/locale/en.yml", __dir__).freeze

Instance Attribute Summary

Attributes inherited from Hanami::Provider::Source

#slice

Instance Method Summary collapse

Methods inherited from Hanami::Provider::Source

#initialize, #target_container

Constructor Details

This class inherits a constructor from Hanami::Provider::Source

Instance Method Details

#prepareObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



27
28
29
30
31
32
33
34
35
# File 'lib/hanami/providers/i18n.rb', line 27

def prepare
  require "i18n"

  SLICE_CONFIGURED_SETTINGS.each do |setting|
    next if config.configured?(setting)

    config.public_send(:"#{setting}=", slice.config.i18n.public_send(setting))
  end
end

#startObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/AbcSize

Since:

  • 0.1.0



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hanami/providers/i18n.rb', line 38

def start
  backend = config.backend || ::I18n::Backend::Simple.new

  # Configure fallbacks if enabled (only for default backend, not custom backends)
  fallbacks_config = nil
  if config.fallbacks && !config.backend
    fallbacks_config =
      case config.fallbacks
      when true
        # Use default_locale as the only fallback
        fallbacks = ::I18n::Locale::Fallbacks.new
        fallbacks.defaults = [config.default_locale]
        fallbacks
      when Hash
        # Use explicit per-locale fallback mapping
        ::I18n::Locale::Fallbacks.new(config.fallbacks)
      when Array
        # Use the given fallbacks for all locales
        fallbacks = ::I18n::Locale::Fallbacks.new
        fallbacks.defaults = config.fallbacks
        fallbacks
      else
        ::I18n::Locale::Fallbacks.new(config.fallbacks)
      end
  end

  # Only load translation files if using the default backend. Custom backends are expected to
  # handle their own initialization.
  unless config.backend
    translation_files = [
      BUNDLED_DEFAULTS_PATH,
      *resolve_load_paths(Array(config.shared_load_path), root: slice.app.root),
      *resolve_load_paths(Array(config.load_path), root: slice.root)
    ].uniq

    backend.load_translations(*translation_files)
  end

  register "i18n", Backend.new(
    backend,
    locale: config.default_locale,
    default_locale: config.default_locale,
    available_locales: config.available_locales,
    fallbacks: fallbacks_config
  )
end