module Traco

Intended to be API compatible with github.com/svenfuchs/i18n/blob/master/lib/i18n/locale/fallbacks.rb

Constants

COLUMN_RE
VERSION

Public Class Methods

column(attribute, locale) click to toggle source

@example

Traco.column("title", :sv)      # => :title_sv
Traco.column("title", :"pt-BR") # => :title_pt_br
# File lib/traco.rb, line 22
def self.column(attribute, locale)
  normalized_locale = locale.to_s.downcase.sub("-", "_")
  "#{attribute}_#{normalized_locale}".to_sym
end
locale_name(locale) click to toggle source
# File lib/traco.rb, line 48
def self.locale_name(locale)
  default = locale.to_s.upcase.sub("_", "-")
  I18n.t(locale, scope: :"i18n.languages", default: default)
end
locale_with_fallbacks(locale, fallback_option) click to toggle source
# File lib/traco.rb, line 53
def self.locale_with_fallbacks(locale, fallback_option)
  locale_fallbacks_resolver = LocaleFallbacks.new(fallback_option)
  locale_fallbacks_resolver[locale]
end
split_localized_column(column) click to toggle source

@example

Traco.split_localized_column("title_sv")     # => [:title, :sv]
Traco.split_localized_column("title_pt_br")  # => [:title, :"pt-BR"]
Traco.split_localized_column("unlocalized")  # => nil
# File lib/traco.rb, line 31
def self.split_localized_column(column)
  match_data = column.to_s.match(COLUMN_RE)
  return unless match_data

  attribute       = match_data[:attribute]
  primary_locale  = match_data[:primary]
  extended_locale = match_data[:extended]

  if extended_locale
    locale = "#{primary_locale}-#{extended_locale.upcase}"
  else
    locale = primary_locale
  end

  [ attribute.to_sym, locale.to_sym ]
end