Javascript 中 ISO 639-1 代码中的语言名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10997128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:45:50  来源:igfitidea点击:

Language name from ISO 639-1 code in Javascript

javascriptinternationalization

提问by aberaud

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.

该网站大量使用 Javascript,与各种元素相关的语言信息在内部被视为 ISO 639-1 代码。

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

如何以用户的语言显示语言名称列表?

采纳答案by Hiroshi Ichikawa

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 CLDRdata.

如果你想要任意语言中任意语言的名称(例如,如何用日语说“韩国语”),你可以使用Unicode CLDR数据。

To use it in JavaScript, you may use cldr NPM packagelike:

要在 JavaScript 中使用它,您可以使用cldr NPM 包,例如:

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.

但不确定该包是否仅支持 Node.js 或还支持浏览器。如果没有,您可以搜索其他库或编写自己的代码来直接解析 CLDR。

回答by Anatoly Mironov

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:

stackoverflow 上也有一些类似的问题。我需要一个 javascript 函数来获取不同语言的英文名称和本地名称。我在 stackoverflow(基于wikipedia找到了一个很好的json 格式的 ISO 693-1 语言代码列表,并创建了一个包含两个函数 getLanguageName 和 getLanguageNativeName 的要点。以下是如何使用它:

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

我用它来回答另一个类似的问题:生成带有谷歌翻译链接的本地化语言名称列表

回答by Clafou

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.

我认为您必须为您希望支持的每种语言维护自己的本地语言名称映射列表。但看起来 Wikipedia正是您所需要的

回答by Hiroshi Ichikawa

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

另一种解决方案是使用iso-639-1 包

Installation:

安装:

npm install iso-639-1

Usage in Node.js:

在 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>

回答by roger janety

Best Way:

最好的办法:

 <script>
    var language = window.navigator.userLanguage || window.navigator.language; 
    alert(language);
    </script>