Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cldr.Territory.from_subdivision_code/2 should resolve aliases #33

Closed
kipcole9 opened this issue Jul 13, 2023 · 3 comments · Fixed by #34
Closed

Cldr.Territory.from_subdivision_code/2 should resolve aliases #33

kipcole9 opened this issue Jul 13, 2023 · 3 comments · Fixed by #34

Comments

@kipcole9
Copy link
Collaborator

Copied from elixir-cldr/cldr#209

Summary

When resolving the names associated with a subdivision, such as uspr, no name is returned even though it's a valid subdivision code. This is because uspr is an alias to the territory PR (Puerto Rico). It can also be the case that a subdivision is deprecated and therefore it is also aliased.

CLDR provides a database of aliases that is returned by Cldr.Config.aliases/0 which is a map, including the key :subdivision under which the subdivision aliases are stored.

This issue is to request that Cldr.Territory.from_subdivision_code/2 resolve aliases to either base subdivision or, in situations like the above, territory code.

Suggested approach

  1. Add Cldr.Territory.subdivision_aliases/0 which encapsulates the alias information. This information can be obtained at compile time from Cldr.Config.aliases[:subdivision]
  2. Which resolving a subdivision, recursively resolve the subdivision name through the aliases until no more aliases are found.
  3. If the result is a territory code (an uncased atom) then use that territory code. If the result is a string its a subdivision code, use the subdivision code.
  4. Resolve the territory name - subdivision or territory

Subdivision alias data

Today, Cldr.Config.aliases/0 will return subdivision aliases as strings no matter whether they are territories or subdivisions. By definition, territories should be of the type t:Cldr.Locale.territory_code/0 which is an uncased atom. The following code is suggested to overcome this bug (which will be fixed in the next ex_cldr release:

  @subdivision_aliases Cldr.Config.aliases()
    |> Map.fetch!(:subdivision)
    |> Enum.map(fn 
      {k, v} when is_binary(v) ->
        if String.length(v) == 2, do: {k, String.to_atom(v)}, else: {k, v}
      other -> other
    end)
    |> Map.new()
    
  def subdivision_aliases do
    @subdivision_aliases
  end
Schultzer added a commit that referenced this issue Jul 13, 2023
This PR closes #33, by resolving subdivision aliases.
@Schultzer
Copy link
Collaborator

@tomciopp I've opened a PR that resolves the subdivision aliases. I would be pleased if you could try out the branch and see if it works for you.

{:ex_cldr_territories, github: "Schultzer/cldr_territories",  branch: "resolve-subdivision-aliases"}
{:ex_cldr_territories, github: "Schultzer/cldr_territories",  ref: "809349580526ed8eff1d6e4a557fdf4110d20c7d"}

@tomciopp
Copy link
Contributor

@Schultzer Yes, this code now works as expected.

Schultzer added a commit that referenced this issue Jul 14, 2023
* Resolve subdivision aliases

This PR closes #33, by resolving subdivision aliases.

* Allow style to be passed to from_subdivision_code
@Schultzer
Copy link
Collaborator

@Schultzer Yes, this code now works as expected.

Thanks, I've pushed the changes to master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants