Module: Solis::LanguageTag

Defined in:
lib/solis/language_tag.rb

Overview

Normalizes language tags so the same language always ends up under the same key, no matter if it came from a 'Label_XX' sheet column or from a parsed rdfs:label (RDF.rb lowercases language tags, ex. 'nl-BE' is read back as :'nl-be')

Class Method Summary collapse

Class Method Details

.normalize(tag) ⇒ Object

'nl' -> :nl, 'nl_be' -> :'nl-BE', 'zh_hant' -> :'zh-Hant'



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/solis/language_tag.rb', line 7

def self.normalize(tag)
  return nil if tag.nil? || tag.to_s.strip.empty?

  primary, *subtags = tag.to_s.strip.split(/[-_]/)
  subtags.map! do |subtag|
    case subtag.length
    when 2 then subtag.upcase
    when 4 then subtag.capitalize
    else subtag.downcase
    end
  end

  ([primary.downcase] + subtags).join('-').to_sym
end