class TwitterCldr::Transforms::TransformId

Constants

CHAIN

Attributes

source[R]
target[R]
variant[R]

Public Class Methods

find(source_locale_or_str, target_locale_or_str) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 18
def find(source_locale_or_str, target_locale_or_str)
  source_locale = parse_locale(source_locale_or_str)
  target_locale = parse_locale(target_locale_or_str)
  source_chain = map_chain_for(source_locale)
  target_chain = map_chain_for(target_locale)
  variants = variants_for(source_locale, target_locale)

  # add original locale strings to chain in case they aren't actually
  # locales (think 'hiragana', etc)
  source_chain << [source_locale_or_str.to_s]
  target_chain << [target_locale_or_str.to_s]

  find_in_chains(
    source_chain, target_chain, variants
  )
end
join(source, target, variant = nil) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 48
def join(source, target, variant = nil)
  base = "#{source}-#{target}"
  variant ? "#{base}/#{variant}" : base
end
join_file_name(parts) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 53
def join_file_name(parts)
  parts.compact.join('-')
end
new(source, target, variant = nil) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 146
def initialize(source, target, variant = nil)
  @source = source
  @target = target
  @variant = variant
end
parse(str) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 35
def parse(str)
  if normalized = normalize(str)
    new(*split(normalized))
  else
    raise InvalidTransformIdError,
      "'#{str}' is not a valid transform id"
  end
end
split(str) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 44
def split(str)
  str.split(/[\-\/]/)
end
transform_id_map() click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 57
def transform_id_map
  @transform_id_map ||= TwitterCldr.get_resource(
    *%w(shared transforms transform_id_map)
  )
end

Private Class Methods

find_in_chains(source_chain, target_chain, variants) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 92
def find_in_chains(source_chain, target_chain, variants)
  variants.each do |variant|
    target_chain.each do |target|
      source_chain.each do |source|
        source_str = join_subtags(source, variant)
        target_str = join_subtags(target, variant)
        transform_id_str = join(source_str, target_str)

        if Transformer.exists?(transform_id_str)
          return parse(transform_id_str)
        end
      end
    end
  end
  nil
end
join_subtags(tags, variant) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 109
def join_subtags(tags, variant)
  tags.join('_').tap do |result|
    result << "_#{variant}" if variant
  end
end
laddered_fallback1(locale) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 135
def laddered_fallback1(locale)
  [locale.language, locale.region]
end
laddered_fallback2(locale) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 139
def laddered_fallback2(locale)
  [locale.full_script]
end
map_chain_for(locale) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 119
def map_chain_for(locale)
  CHAIN.map { |link| send(link, locale) }
end
normal_fallback1(locale) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 123
def normal_fallback1(locale)
  [locale.language, locale.full_script, locale.region]
end
normal_fallback2(locale) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 127
def normal_fallback2(locale)
  [locale.language, locale.full_script]
end
normal_fallback3(locale) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 131
def normal_fallback3(locale)
  [locale.language]
end
normalization_index() click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 81
def normalization_index
  @index ||=
    transform_id_map.each_with_object({}) do |(key, file), ret|
      source, target, variant = split(key)
      key = join(source, target, variant)
      reverse_key = join(target, source, variant)
      ret[key.downcase] = key
      ret[reverse_key.downcase] = reverse_key
    end
end
normalize(str) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 74
def normalize(str)
  source, target, variant = split(str)
  normalization_index[
    join(source, target, variant).downcase
  ]
end
parse_locale(locale_or_str) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 65
def parse_locale(locale_or_str)
  case locale_or_str
    when TwitterCldr::Shared::Locale
      locale_or_str
    else
      TwitterCldr::Shared::Locale.parse(locale_or_str.to_s).maximize
  end
end
variants_for(source_locale, target_locale) click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 115
def variants_for(source_locale, target_locale)
  (source_locale.variants + target_locale.variants + [nil]).uniq
end

Public Instance Methods

file_name() click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 160
def file_name
  self.class.transform_id_map[to_s]
end
has_variant?() click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 152
def has_variant?
  !!variant
end
reverse() click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 156
def reverse
  self.class.new(target, source, variant)
end
to_s() click to toggle source
# File lib/twitter_cldr/transforms/transform_id.rb, line 164
def to_s
  self.class.join(source, target, variant)
end