Module: Traco

Defined in:
lib/traco/locale_fallbacks.rb,
lib/traco.rb,
lib/traco/version.rb,
lib/traco/attributes.rb,
lib/traco/translates.rb,
lib/traco/class_methods.rb

Overview

Defined Under Namespace

Modules: ClassMethods, Translates Classes: Attributes, LocaleFallbacks

Constant Summary collapse

COLUMN_RE =
/\A
  (?<attribute>\w+?)        # title
  _                         # _
  (?<primary>[a-z]{2})      # pt
  (
    _                       # _
    (?<extended>[a-z]{2})   # br
  )?
\z/x
VERSION =
"5.1.0"

Class Method Summary collapse

Class Method Details

.column(attribute, locale) ⇒ Object

Examples:

Traco.column("title", :sv)      # => :title_sv
Traco.column("title", :"pt-BR") # => :title_pt_br


22
23
24
25
# 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) ⇒ Object



48
49
50
51
# 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) ⇒ Object



53
54
55
56
# 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) ⇒ Object

Examples:

Traco.split_localized_column("title_sv")     # => [:title, :sv]
Traco.split_localized_column("title_pt_br")  # => [:title, :"pt-BR"]
Traco.split_localized_column("unlocalized")  # => nil


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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