Sha256: e66a983df730448997c9a6ccaeef429e59e72010c1a37198156d91e8287b9ce0
Contents?: true
Size: 1.5 KB
Versions: 7
Compression:
Stored size: 1.5 KB
Contents
# frozen_string_literal: true # Encapsulates the translation information for all locales, for a single field in a single country require "yaml" module Worldwide class Field VALID_KEYS = [ :first_name, :last_name, :company, :address1, :address2, :city, :province, :zip, :country, :phone, ] def initialize(country_code:, field_key:) @country_code = country_code.upcase.to_sym @field_key = field_key.downcase.to_sym end def autofill(locale: I18n.locale) lookup("autofill", locale: locale) end def label(locale: I18n.locale) lookup("label.default", locale: locale) end def label_marked_optional(locale: I18n.locale) lookup("label.optional", locale: locale) end def error(locale: I18n.locale, code:, options: {}) lookup("errors.#{code}", locale: locale, options: options) end private def base_key(country_key) "worldwide.#{country_key}.addresses.#{@field_key}" end def default_lookup(key_suffix, options: {}) base = base_key("_default") I18n.t("#{base}.#{key_suffix}", default: nil, **options) || I18n.with_locale(:en) { I18n.t("#{base}.#{key_suffix}", default: nil, **options) } end def lookup(key_suffix, locale:, options: {}) I18n.with_locale(locale) do I18n.t("#{base_key(@country_code)}.#{key_suffix}", default: nil, **options) || default_lookup(key_suffix, options: options) end end end end
Version data entries
7 entries across 7 versions & 1 rubygems