diff --git a/CHANGELOG.md b/CHANGELOG.md index f372af8..eb33cc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Add `WeekData` component, [#229](https://github.com/ruby-i18n/ruby-cldr/pull/229) - Drop support for Ruby 2, [#265](https://github.com/ruby-i18n/ruby-cldr/pull/265) - Drop support for Ruby 3.0, [#268](https://github.com/ruby-i18n/ruby-cldr/pull/268) +- Add `LanguageInfo` component --- diff --git a/lib/cldr/export.rb b/lib/cldr/export.rb index 3942c88..4ef1954 100644 --- a/lib/cldr/export.rb +++ b/lib/cldr/export.rb @@ -26,6 +26,7 @@ module Export :Aliases, :CountryCodes, :CurrencyDigitsAndRounding, + :LanguageInfo, :LikelySubtags, :Metazones, :NumberingSystems, diff --git a/lib/cldr/export/data.rb b/lib/cldr/export/data.rb index 4c0a632..ed9aa78 100644 --- a/lib/cldr/export/data.rb +++ b/lib/cldr/export/data.rb @@ -16,6 +16,7 @@ module Data autoload :Delimiters, "cldr/export/data/delimiters" autoload :Fields, "cldr/export/data/fields" autoload :Languages, "cldr/export/data/languages" + autoload :LanguageInfo, "cldr/export/data/language_info" autoload :Layout, "cldr/export/data/layout" autoload :LikelySubtags, "cldr/export/data/likely_subtags" autoload :Lists, "cldr/export/data/lists" diff --git a/lib/cldr/export/data/language_info.rb b/lib/cldr/export/data/language_info.rb new file mode 100644 index 0000000..6d9fc1a --- /dev/null +++ b/lib/cldr/export/data/language_info.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +module Cldr + module Export + module Data + class LanguageInfo < Base + def initialize + super(nil) + update(language_matching: language_matching) + deep_sort! + end + + private + + def language_matching + doc.xpath("//languageMatching/languageMatches").each_with_object({}) do |matches_node, ret| + matching_type = matches_node.attribute("type").to_s.underscore.to_sym + ret[matching_type] = { + paradigm_locales: paradigm_locales(matches_node), + match_variables: match_variables(matches_node), + language_matches: language_matches(matches_node), + } + end + end + + def paradigm_locales(matches_node) + locales_node = matches_node.xpath("./paradigmLocales").first + return [] unless locales_node + + locales_str = locales_node.attribute("locales").to_s + locales_str.split(" ").map { |locale| Cldr::Export.to_i18n(locale).to_s } + end + + def match_variables(matches_node) + matches_node.xpath("./matchVariable").each_with_object({}) do |var_node, hash| + id = var_node.attribute("id").to_s + value = var_node.attribute("value").to_s + hash[id] = value + end + end + + def language_matches(matches_node) + matches_node.xpath("./languageMatch").map do |match_node| + { + desired: Cldr::Export.to_i18n(match_node.attribute("desired")).to_s, + supported: Cldr::Export.to_i18n(match_node.attribute("supported")).to_s, + distance: match_node.attribute("distance").to_s.to_i, + oneway: match_node.attribute("oneway").to_s == "true", + }.reject { |_, v| v == false } + end + end + end + end + end +end diff --git a/lib/cldr/export/deep_validate_keys.rb b/lib/cldr/export/deep_validate_keys.rb index e4a9a79..b3be6e0 100644 --- a/lib/cldr/export/deep_validate_keys.rb +++ b/lib/cldr/export/deep_validate_keys.rb @@ -25,6 +25,10 @@ def validate(hash, component, key_path = []) CountryCodes: [["country_codes", "."]], CurrencyDigitsAndRounding: [["."]], Fields: [["*", "relative", "."]], + LanguageInfo: [ + ["language_matching", ".", "match_variables", "."], + ["language_matching", ".", "paradigm_locales", "."], + ], Languages: [["languages", "."]], LikelySubtags: [["subtags", "."]], Metazones: [