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

Allow for multiple possible spellings of names of a country #174

Closed
danilofuchs opened this issue May 26, 2020 · 2 comments · Fixed by #175
Closed

Allow for multiple possible spellings of names of a country #174

danilofuchs opened this issue May 26, 2020 · 2 comments · Fixed by #175

Comments

@danilofuchs
Copy link
Contributor

Currently, the localisation JSON maps strings 1-1. But in some languages, people may spell the country name differently.

I use this library to parse an API response into Alpha-2 codes. But, this external API sometimes uses slightly different spellings from those in this lib, but are completely valid spellings.

For instance, in Portuguese, mapping US to Estados Unidos da América is as valid as Estados Unidos.

Currently, I have to manually change the localisation file with different values, suited for my use-case or create a hard-coded matching algorithm.

I suggest we allow the possibility of accepting multiple names for a country in a given language. For instance, langs/pt.json#L72 would map to an array:

- "US": "Estados Unidos",
+ "US": ["Estados Unidos", "Estados Unidos da América"],

With this proposed syntax, if I run countries.getName("US", "pt"), the lib would return me the first entry in the array ("Estados Unidos"). But, when I do the opposite, either name would be valid.

countries.getName("US", "pt") // Estados Unidos
countries.getAlpha2Code("Estados Unidos da América", "pt")) // US
countries.getAlpha2Code("Estados Unidos", "pt")) // US

or, to avoid confusion on array ordering, we could use a more complex object:

- "US": "Estados Unidos",
+ "US": {
+    "default": "Estados Unidos",
+    "alternatives": ["Estados Unidos da América"]
+ },

Checking the current implementation, it doesn't seem difficult to accomplish. getAlpha2Code already searches the entire array of translations. It's just a matter of running

// TODO: Perform lowerCase on array or diacritics removal
Array.isArray(codenames[p]) ? codenames[p].includes(name) : codenames[p] === name
@michaelwittig
Copy link
Owner

I prefer

"US": ["Estados Unidos", "Estados Unidos da América"],

for countries where multiple names exist. For countries with a single name we can stick with what we have.

Feel free to create a PR, I will merge it.

@jsgoupil
Copy link

jsgoupil commented Jun 8, 2020

This is a sad breaking change. At least it's in 6.0.0.
Maybe on the readme, you could show an example that getNames might not return just a string.
I have to do something with type matching like this. It's a little painful :(

let c = countries.getNames('en');
this.options = Object.keys(c)
    .filter(m => ignoreCountries.indexOf(m) === -1)
    .map(code => {
        let obj = c[code];
        return {
            id: code,
            text: this.isArray(obj) ? obj[0] : obj
        };
    });

private isArray(c: string | string[]): c is string[] {
    return Array.isArray(c);
}

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