17

I'm building a website where people can associate a language information to content.

The website uses Javascript heavily and the language information associated to various elements is treated internally as an ISO 639-1 code.

How to show a list of language names - in the language of the user ?

aberaud
  • 909
  • 1
  • 11
  • 24

7 Answers7

26

There is a native support for this in the new(ish) Intl API:

let languageNames = new Intl.DisplayNames(['en'], {type: 'language'});
languageNames.of('fr');      // "French"
languageNames.of('de');      // "German"
languageNames.of('fr-CA');   // "Canadian French"
Michal Bryxí
  • 1,166
  • 1
  • 11
  • 18
15

There are some similar questions on stackoverflow. I needed a javascript function for getting English names and Native names for different languages. I found a nice json formatted list of ISO 693-1 language codes on stackoverflow (based on wikipedia) and created a gist with two functions getLanguageName and getLanguageNativeName. Here is how to use it:

getLanguageNativeName("cv"); // --> "чӑваш чӗлхи"
getLanguageName("cv"); // --> "Chuvash"
getLanguageNativeName("cv-RU"); // --> "чӑваш чӗлхи"
getLanguageName("cv-RU"); // --> "Chuvash"

I used it to answer another similar question: generate a list of localized language names with links to google translate

Community
  • 1
  • 1
Anatoly Mironov
  • 7,356
  • 1
  • 26
  • 27
  • This is almost it. But it only shows the language name in two languages. For instance, there is a dropbown on my site to associate a language to some content. If a french guy uses my site and want to select "german", it should show "allemand" (french name for the german language). Same for a chinese visitor etc. – aberaud Aug 19 '13 at 09:53
4

If you want a name of an arbitrary language in an arbitrary language (e.g, how to say "Korean language" in Japanese), you can use Unicode CLDR data.

To use it in JavaScript, you may use cldr NPM package like:

cldr.extractLanguageDisplayNames('it').en;
# => 'inglese'

But not sure if the package only supports Node.js or also supports browsers. If not, you may search for other libraries or write your own code to parse CLDR directly.

Hiroshi Ichikawa
  • 616
  • 9
  • 10
3

I think you are stuck with having to maintain your own list of mappings to native language names for each of the languages you wish to support. But it looks like Wikipedia has just what you need.

Clafou
  • 15,250
  • 7
  • 58
  • 89
  • What I need would be some kind of web API or JS library. The wikipedia list doesn't provide the language name in different languages :-( – aberaud Jun 12 '12 at 15:19
  • 1
    Ah I see, sorry, I misread and thought you wanted the names in the corresponding language (which Wikipedia has). It seems to be fairly common to present them this way though. But for your requirement, I suppose you have to localize the list of languages, like you localize any of your other UI. – Clafou Jun 12 '12 at 15:27
2

Another solution is to use iso-639-1 package.

Installation:

npm install iso-639-1

Usage in Node.js:

const ISO6391 = require('iso-639-1')
console.log(ISO6391.getAllCodes())  // ['aa', 'ab', ...]
console.log(ISO6391.getName('cv'))  // 'Chuvash'
console.log(ISO6391.getNativeName('cv'))  // 'чӑваш чӗлхи'

Usage in browsers:

<script type="text/javascript" src="./node_modules/iso-639-1/build/index.js"></script>
<script>
  console.log(ISO6391.getAllCodes())  // ['aa', 'ab', ...]
  console.log(ISO6391.getName('cv'))  // 'Chuvash'
  console.log(ISO6391.getNativeName('cv'))  // 'чӑваш чӗлхи'
</script>
Hiroshi Ichikawa
  • 616
  • 9
  • 10
  • Nice, but unfortunately still not allowing to show "Chinese" in german, that would be "Chinesisch". This library only allows to get the language name in english (for some reason) and in the native language. – aberaud Apr 18 '18 at 14:51
0

For the reverse of Michal Bryxí's answer, using Intl.DisplayNames to get the ISO 639-1 code from the language name (e.g. Englishen):

function generateLangMap () {
  const langNames = new Intl.DisplayNames(['en'], {type: 'language'})
  const langMap = {}
  for (let i = 0; i < 26; i++) {
    for (let j = 0; j < 26; j++) {
      let code = String.fromCharCode(97 + i) + String.fromCharCode(97 + j)
      let name = langNames.of(code)
      if (name !== code) {
        langMap[name] = code
      }
    }
  }
  const langMap2 = {
    // Avoid using deprecated codes:
    'Akan': 'ak',
    'Hebrew': 'he',
    'Indonesian': 'id',
    'Javanese': 'jv',
    'Romanian': 'ro',
    'Yiddish': 'yi',
    // Optional extras:
    'Tagalog': 'tl',
  }
  return { ...langMap, ...langMap2 }
}

// Usage:
const langMap = generateLangMap()
console.log(langMap['English']) // 'en'
console.log(langMap['Chinese']) // 'zh'
console.log(langMap['Spanish']) // 'es'
Hat
  • 540
  • 8
  • 25
-2

Best Way:

 <script>
    var language = window.navigator.userLanguage || window.navigator.language; 
    alert(language);
    </script>
roger janety
  • 61
  • 2
  • 8