Class: LanguageTag

Inherits:
Object
  • Object
show all
Includes:
PartiallyComparable, Yast::Logger
Defined in:
src/lib/language_tag.rb

Overview

Language tags like “cs” “cs_CZ” “cs_CZ.UTF-8”.

FIXME: improve the simplistic string comparisons

Instance Method Summary collapse

Methods included from PartiallyComparable

#<, #<=, #==, #>, #>=

Constructor Details

#initialize(s) ⇒ LanguageTag


57
58
59
# File 'src/lib/language_tag.rb', line 57

def initialize(s)
  @tag = s
end

Instance Method Details

#<=>(other) ⇒ Object

Like with classes (where Special < General) “en_US” < “en” Mnemonics: number of speakers


69
70
71
72
73
74
# File 'src/lib/language_tag.rb', line 69

def <=>(other)
  return 0 if to_s == other.to_s
  return -1 if to_s.start_with?(other.to_s)
  return 1 if other.to_s.start_with?(to_s)
  nil
end

#generalizeLanguageTag?

A more general tag: “en_US” -> “en” (-> nil)


78
79
80
81
82
# File 'src/lib/language_tag.rb', line 78

def generalize
  self.class.new(@tag.split("_").first) if @tag.include? "_"
  # else nil
  # FIXME: or self, find out what makes more sense
end

#name(lang_map_cache: nil) ⇒ String?


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'src/lib/language_tag.rb', line 85

def name(lang_map_cache: nil)
  force_retranslation = false
  lang_map_cache ||= Yast::Language.GetLanguagesMap(force_retranslation)
  attrs = lang_map_cache[@tag]
  if attrs.nil?
    # we're en, find en_US
    _tag, attrs = lang_map_cache.find { |k, _v| self > LanguageTag.new(k) }
  end
  if attrs.nil?
    log.warn "Could not find name for language '#{@tag}'"
    return nil
  end

  attrs[4]
end

#to_sObject


61
62
63
# File 'src/lib/language_tag.rb', line 61

def to_s
  @tag
end