Skip to content

Commit

Permalink
Add language data and matching
Browse files Browse the repository at this point in the history
  • Loading branch information
apainintheneck committed Nov 29, 2023
1 parent d5c66c4 commit 547eb27
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
31 changes: 31 additions & 0 deletions lib/atlasq/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,37 @@ def self.all_currencies
end
end

# @param language_code [String] ISO639 language code
# @return [ISO_639]
def self.language_by_code(language_code)
@language ||= {}
@language[language_code] ||= ISO_639.find(language_code)
end

# @param term [String]
# @return [Array<ISO3166::Country>]
def self.languages(term)
language = language_by_code(term)
return [] if language.nil?

language_code = language.alpha2
return [] if language_code.empty?

all_languages.fetch(language_code, [])
end

# @return [Hash<String, ISO3166::Country>] ISO639 language code to country
def self.all_languages
@all_languages ||= all_countries
.each_with_object({}) do |country, hash|
country.languages.each do |language_code|
hash[language_code] ||= []
hash[language_code] << country
end
end
.freeze
end

# @param number [String] ISO3166-1 numeric country code
# @return [String, nil]
def self.emoji_flag(iso_number)
Expand Down
8 changes: 4 additions & 4 deletions lib/atlasq/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def self.country(country, search_term)
attributes: Format.one_line_country(country),
info: {
"Search Term" => search_term,
"Languages" => Format.languages(country.languages),
"Languages" => Format.language_names(country.languages),
"Nationality" => country.nationality,
"Region" => country.subregion,
"Continent" => country.continent,
Expand All @@ -128,11 +128,11 @@ def self.country(country, search_term)
# @example "English / Shona / Ndebele, North; North Ndebele"
# @param language_codes [Array<String>] Ex. ["id"]
# @return [String]
def self.languages(language_codes)
def self.language_names(language_codes)
language_codes
.take(4) # arbitrary limit to avoid long lines
.map do |lang|
ISO_639.find(lang).english_name
.map do |language_code|
Data.language_by_code(language_code).english_name
end
.join(" / ")
end
Expand Down
16 changes: 16 additions & 0 deletions lib/atlasq/partial_match.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "iso-639"

module Atlasq
module PartialMatch
# @param term [String]
Expand Down Expand Up @@ -39,5 +41,19 @@ def self.currencies(term)

@currencies.search(term)
end

# @param term [String]
# @return [Array<String>] 3 letter language codes (ISO639)
def self.languages(term)
@languages ||= begin
language_data = (ISO_639::ISO_639_1 | ISO_639::ISO_639_2)
id_to_language_names = language_data
.to_h { |language| [language.alpha3, [language.english_name, language.french_name]] }

Util::WordMap.new(id_to_language_names)
end

@languages.search(term)
end
end
end

0 comments on commit 547eb27

Please sign in to comment.