Coder Social home page Coder Social logo

Comments (5)

papandreou avatar papandreou commented on June 1, 2024 1

Oh, now I understand. It sounds like you could use bracket notation instead of dot notation for extracting from the object? Like this:

var locales = cldr.localeIds
var languageJsonlist = []
for (var i=0; i < locales.length; i++) {
    var locale = locales[i]
    logger.info(locale)
    logger.info(cldr.extractLanguageDisplayNames('en')[locale])
}

from node-cldr.

papandreou avatar papandreou commented on June 1, 2024 1

Basically the object returned by cldr.extractLanguageDisplayNames(...) is exactly the data found in CLDR. In this case it means that there are only display names available for fr (generic French), fr_CA (Canadian French), and fr_CH (Swiss French):

<language type="fr">French</language>
<language type="fr_CA">Canadian French</language>
<language type="fr_CH">Swiss French</language>

I don't know why it's exactly that set of "sublocales" that are included. I guess that means that you'll have to fall back to the "non-suffixed" version so that fr_fr and fr_foobar get a display name of just "French".

Something like:

function lookupDisplayNameForLocale(targetLocaleId, localeId) {
    var languageDisplayNames = cldr.extractLanguageDisplayNames(targetLocaleId);
    var normalizedLocaleFragments = localeId.split(/[_-]/).map(function (fragment) {
        return fragment.toLowerCase();
    });
    for (var i = normalizedLocaleFragments.length ; i > 0 ; i -= 1) {
        var translation = languageDisplayNames[normalizedLocaleFragments.slice(0, i).join('_')];
        if (translation) {
            return translation;
        }
    }
    // No translation found at all :(
}

console.log(lookupDisplayNameForLocale('en', 'fr_fr')); // French

from node-cldr.

papandreou avatar papandreou commented on June 1, 2024

I'm not sure I understand the question? Could you post an example of the sort of output you need?

In general the extract* functions return values that are quite close to the CLDR XML source, so for most use cases it will require some postprocessing similar to what you already tried.

from node-cldr.

weeco avatar weeco commented on June 1, 2024

I am expecting full language name for each locale, like this:

5:41:29 PM - info: zh
5:41:29 PM - info: Chineese

This line is my problem:
cldr.extractLanguageDisplayNames('en').locale

Apparently I can't use a dynamical "locale" (which is a string like 'en', 'de', 'zh') and I am not sure how I can workaround this?

from node-cldr.

weeco avatar weeco commented on June 1, 2024

Thanks a lot that was exactly what I was looking for. I am sorry for another question but right after testing I noticed that a lot "language display names" aren't set. Is there a reason for this?

For example:
cldr.extractLanguageDisplayNames('en').fr_fr

I expected that it would output French (France) or simply French. Same applies to de_be, de_it, de_de. Is it just because these languages aren't popular enough or so?

from node-cldr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.