Sha256: 12ec7d9afd813e26765b4eb588085381d8cc08ed361b39b040eeddfe3015390a

Contents?: true

Size: 975 Bytes

Versions: 6

Compression:

Stored size: 975 Bytes

Contents

# frozen_string_literal: true

# Transalte with array of translation
#
#   => UiBibz::Utils::Internationalization.new("ui_bibz.table.actions.#{ @store.model.to_s.underscore }.show", default: defaults).translate
module UiBibz::Utils
  # Internalizations methods to translate can be used outside of Ui Bibz
  class Internationalization
    def initialize(translation, options = {})
      @translation = translation
      @options     = options
    end

    def translate
      I18n.t(@translation, options_with_default)
    end

    private

    def options_with_default
      @options[:default] = translate_default unless @options[:default].nil?
      @options
    end

    # To know if translation missing
    def i18n_set?(key)
      I18n.t key, raise: true
    rescue StandardError
      false
    end

    def translate_default
      I18n.t([*@options[:default]].select { |translation| i18n_set? translation }.first, default: [*@options[:default]].last)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ui_bibz-2.5.6 lib/ui_bibz/utils/internationalization.rb
ui_bibz-2.5.5 lib/ui_bibz/utils/internationalization.rb
ui_bibz-2.5.3 lib/ui_bibz/utils/internationalization.rb
ui_bibz-2.5.2 lib/ui_bibz/utils/internationalization.rb
ui_bibz-2.5.1 lib/ui_bibz/utils/internationalization.rb
ui_bibz-2.5.0 lib/ui_bibz/utils/internationalization.rb