Skip to content

Commit

Permalink
Fix wildcard locale matcher. Closes #225
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Apr 1, 2024
1 parent 978b98e commit a2946a4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
40 changes: 23 additions & 17 deletions lib/cldr/config/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1543,7 +1543,7 @@ defmodule Cldr.Config do
:"fr-KM", :"fr-LU", :"fr-MA", :"fr-MC", :"fr-MF", :"fr-MG", :"fr-ML",
:"fr-MQ", :"fr-MR", :"fr-MU", :"fr-NC", :"fr-NE", :"fr-PF", :"fr-PM",
:"fr-RE", :"fr-RW", :"fr-SC", :"fr-SN", :"fr-SY", :"fr-TD", :"fr-TG",
:"fr-TN", :"fr-VU", :"fr-WF", :"fr-YT", :frr
:"fr-TN", :"fr-VU", :"fr-WF", :"fr-YT"
]
"""
Expand All @@ -1553,30 +1553,36 @@ defmodule Cldr.Config do
...
]
def expand_locale_names(locale_names) do
Enum.map(locale_names, fn locale_name ->
Enum.flat_map(locale_names, fn locale_name ->
locale_name = to_string(locale_name)

if String.contains?(locale_name, @wildcard_matchers) do
case Regex.compile(locale_name) do
{:ok, regex} ->
Enum.filter(all_locale_names(), &match_name?(regex, &1))
case String.split(locale_name, @wildcard_matchers) do
# Special case of locale-*
[locale_base, ""] ->
Enum.filter(all_locale_names(), &String.starts_with?(to_string(&1), locale_base))

{:error, reason} ->
raise ArgumentError,
"Invalid regex in locale name #{inspect(locale_name)}: #{inspect(reason)}"
end
else
canonical_name(locale_name)
# No wildcard
[_locale] ->
[canonical_name(locale_name)]

# Multiple splits so treat it as a regex
_other ->
case Regex.compile(locale_name) do
{:ok, regex} ->
Enum.filter(all_locale_names(), &match_name?(regex, &1))

{:error, reason} ->
raise ArgumentError,
"Invalid regex in locale name #{inspect(locale_name)}: #{inspect(reason)}"
end
end
end)
|> List.flatten()
|> Enum.map(fn locale_name ->
|> Enum.flat_map(fn locale_name ->
case String.split(to_string(locale_name), "-") do
[language] -> String.to_atom(language)
[language] -> [String.to_atom(language)]
[language | _rest] -> [String.to_atom(language), locale_name]
end
end)
|> List.flatten()
|> Enum.uniq()
end

Expand All @@ -1587,7 +1593,7 @@ defmodule Cldr.Config do
def canonical_name(locale_name) do
name =
locale_name
|> locale_name_from_posix
|> locale_name_from_posix()
|> String.downcase()

Map.get(known_locales_map(), name, locale_name)
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
"benchee": {:hex, :benchee, "1.3.0", "f64e3b64ad3563fa9838146ddefb2d2f94cf5b473bdfd63f5ca4d0657bf96694", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "34f4294068c11b2bd2ebf2c59aac9c7da26ffa0068afdf3419f1b176e16c5f81"},
"cldr_utils": {:hex, :cldr_utils, "2.24.2", "364fa30be55d328e704629568d431eb74cd2f085752b27f8025520b566352859", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:decimal, "~> 1.9 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "3362b838836a9f0fa309de09a7127e36e67310e797d556db92f71b548832c7cf"},
"cldr_utils": {:hex, :cldr_utils, "2.25.0", "3cc2ab6e9e4f855ba78a3f3fc4963ccf7b68b731f4e91de3d9b310adddb96b62", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:decimal, "~> 1.9 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "9041660356ffa1129e0d87d110e188f5da0e0bba94fb915e11275e04ace066e1"},
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
"dialyxir": {:hex, :dialyxir, "1.4.3", "edd0124f358f0b9e95bfe53a9fcf806d615d8f838e2202a9f430d59566b6b53b", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "bf2cfb75cd5c5006bec30141b131663299c661a864ec7fbbc72dfa557487a986"},
Expand Down

0 comments on commit a2946a4

Please sign in to comment.