简体   繁体   English

如何使用 navigator.language 进行本地化

[英]How to use navigator.langugage for localization

I am trying to use navigator.langauge in react and I want to show greeting message in their local language by default and greetings should be according to hour.我正在尝试在 react 中使用 navigator.langauge,我想默认以当地语言显示问候语,问候语应根据小时显示。 Can we use navigator.lang to do so?我们可以使用 navigator.lang 来做到这一点吗?

I have tired react i18 but it doesn't work in chrome extension我已经厌倦了 i18 的反应,但它在 chrome 扩展中不起作用

navigator.language is going to give you a string representing the language and dialect (example: en-US ) that the user's browser is set to. navigator.language将为您提供一个字符串,表示用户浏览器设置的语言和方言(例如: en-US )。 If you want to just grab the language part (not the dialect), you could do this:如果你只想抓住语言部分(而不是方言),你可以这样做:

navigator.language.split('-').shift()

And it should give you just give you the "en" part.它应该只给你“en”部分。

This is a multilingual sample.这是一个多语言样本。

manifest.json清单.json

{
  "name": "__MSG_extName__",
  "version": "1.0",
  "manifest_version": 3,
  "default_locale": "en",
  "action": {
    "default_popup": "popup.html"
  }
}

popup.html弹出窗口.html

<html>
<body style="min-width:400px">
  <div id="hello"></div>
  <script src="popup.js"></script>
</body>
</html>

popup.js弹窗.js

const hello = chrome.i18n.getMessage("hello");
document.getElementById("hello").innerText = hello;

_locales\en\messages.json _locales\en\messages.json

{
  "extName": {
    "message": "Hello."
  },
  "hello": {
    "message": "Hello."
  }
}

_locales\ja\messages.json _locales\ja\messages.json

{
  "extName": {
    "message": "こんにちは。"
  },
  "hello": {
    "message": "こんにちは。"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM