From 32e4032ee60cdd24bba3931e7f7969a6e2439ca5 Mon Sep 17 00:00:00 2001 From: Michael Overmeyer Date: Thu, 28 May 2026 14:56:39 -0400 Subject: [PATCH] Add `LanguageInfo` component --- CHANGELOG.md | 1 + lib/cldr/export.rb | 1 + lib/cldr/export/data.rb | 1 + lib/cldr/export/data/language_info.rb | 55 +++++++++++++++++++++++++++ lib/cldr/export/deep_validate_keys.rb | 4 ++ 5 files changed, 62 insertions(+) create mode 100644 lib/cldr/export/data/language_info.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index f372af8b..eb33cc31 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 3942c887..4ef19546 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 4c0a632a..ed9aa784 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 00000000..6d9fc1aa --- /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 e4a9a796..b3be6e08 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: [