rxaviers / cldr-data-npm

Npm module for Unicode CLDR JSON data
MIT License
42 stars 28 forks source link

language detected. #14

Closed martingg88 closed 9 years ago

martingg88 commented 9 years ago

how can i find the main language used for specific country.

For example if i give "US" as country code. what is the method/files i need to find the main language for "US" country?

rxaviers commented 9 years ago

Take a look at supplemental/territoryInfo.

martingg88 commented 9 years ago

try to review the file. in between, i realize one country may have more than one languagePopulation, so which one of language is more reliable to pick up for specific country? should i based on _populationPercent to pick up the right language?

rxaviers commented 9 years ago

You'll notice there's a field populationPercent that shows the most used one per territory. Please, just let me know in any further questions.

martingg88 commented 9 years ago

cool. but the language code is not consistent in \main\ms-Latn\languages.json.

as we know 'ms_Latn' could be the main language for MY country. However we may not find any 'ms_Latn' in \main\ms-Latn\languages.json (note: refer to localeDisplayNames.languages). Thus we may have some issue to find/match language in any part of project. any idea how to solve this issue?

  "MY": {
    "_gdp": "525000000000",
    "_literacyPercent": "93.1",
    "_population": "30073400",
    "languagePopulation": {
      "ms_Latn": {
        "_officialStatus": "official",
        "_populationPercent": "75"
      },
  },
rxaviers commented 9 years ago

However we may not find any 'ms_Latn' in \main\ms-Latn\languages.json

No, because ms-Latn means ms language, Latn script. Therefore, you look for ms in there.

I suggest you use https://github.com/rxaviers/cldrjs for traversing the data. By using cldrjs, you can:

npm install cldr-data cldrjs
var Cldr = require("cldrjs");
Cldr.load(require("cldr-data/main/ms-Latn/languages"));

var cldr = new Cldr("ms-Latn");
cldr.main("localeDisplayNames/languages/{language}");
// > 'Bahasa Melayu'
martingg88 commented 9 years ago

ok...thanks.

martingg88 commented 9 years ago

@rxaviers i do have one question here. thanks.

how to make sure this ISO format (etc: language-country/en-US) is apply to my project by using this cldr data library?

I feel strange cldr data doesn't follow this format as some languages do not append with country code.

rxaviers commented 9 years ago

@martingg88 I didn't understand your question? Could you reformulate it please?

martingg88 commented 9 years ago

i realize that cldr have some languages that do not append with country code. these languages are as follow:

  1. en
  2. en-US
  3. ms
  4. id.

how to make sure the language always follow this format as follows.

  1. language code - country code (etc: en-US, en-GB)
rxaviers commented 9 years ago

I recommend reading What is a Locale? :)

rxaviers commented 9 years ago

If you are using cldrjs, it takes care of the normalization for you. For example,

var Cldr = require("cldrjs");
Cldr.load(
  require("cldr-data/supplemental/likelySubtags"), // Note you need to load likelySubtags.
  require("cldr-data/main/en/languages")
);

var cldr = new Cldr("en");
cldr.main("localeDisplayNames/languages/{language}");
// > 'English'

var cldr = new Cldr("en-US");
cldr.main("localeDisplayNames/languages/{language}");
// > 'English'

var cldr = new Cldr("en-Latn");
cldr.main("localeDisplayNames/languages/{language}");
// > 'English'

var cldr = new Cldr("en-Latn-US");
cldr.main("localeDisplayNames/languages/{language}");
// > 'English'
martingg88 commented 9 years ago

many thanks for your answer.

  1. var cldr = new Cldr("en-US");

this should produce "American English" instead. It make more sense than "English". right?

  1. last question here.

a. i'm trying to generate a list of language (that is supported by cldr data) for selection purpose. b. i will not get standard language ISO format if i based on cldr.main("localeDisplayNames/languages" ). c. what can i do to generate a list that its language code will follow ISO format (etc: language code-country code)?

en-US = "America English"
en-GB = "UK English"

ms-MY = "Bahasa Melayu"

rxaviers commented 9 years ago

It's very easy to accomplish what you want by iterating over the whole localeDisplayNames/languages. I don't know what you mean by "language ISO format".

martingg88 commented 9 years ago

but some of languages do not follow this format. (language code-country code) refer to ISO format.

any idea how to make sure all languages follow this format (language code-country code) (etc: en-US)

rxaviers commented 9 years ago

Does it help you?

var locale = "en";
var Cldr = require("cldrjs");
Cldr.load(require("cldr-data/main/" + locale + "/languages"));

var cldr = new Cldr(locale);

cldr.main("localeDisplayNames/languages");
// > { aa: "Afar",
//     ab: "Abkhazian",
//     ...
//   }

Object.keys(cldr.main("localeDisplayNames/languages")).map(function(locale) {
  return cldr.main(["localeDisplayNames/languages", locale]);
});
// > [ "Afar",
//     "Abkhazian",
//     ...
//   ]
martingg88 commented 9 years ago

i should be able to generate list like following. But my question is how to make left hand side which is language code to follow this format (language_code-country_code)? so i expect all the language code in following list should be appended with country code (etc: en-US, en-GB, en-SG).

{ aa: "Afar", ab: "Abkhazian", ... }

rxaviers commented 9 years ago

@martingg88 I hope all the above makes it clear on how you could accomplish what you need.

martingg88 commented 9 years ago

anyway. thanks for your effort.