简体   繁体   English

Node.js、Vue.js,读取“HTTP:接受语言”设置“导入”,然后为应用提供服务

[英]Node.js, Vue.js, read 'HTTP: Accept-Language' set 'import' then serve the app

I'm setting a vue.js app with i18n, so I have multiple static translation files for different langues, and tow css files one ltr and the other rtl.我正在使用 i18n 设置 vue.js 应用程序,因此我有多个用于不同语言的静态翻译文件,以及一个 ltr 和另一个 rtl 的两个 css 文件。

For now I'm setting 'en' as default lang and ltr as default css, serve them, then check the user locales and resend the right files with dynamic import, depending on the user preferred lang.现在,我将“en”设置为默认 lang,将 ltr 设置为默认 css,为它们提供服务,然后检查用户语言环境并根据用户首选 lang 重新发送具有动态导入的正确文件。

I've searched a lot, but can't find a way to read the 'HTTP: Accept-Language' set the 'import' then serve the app with the right files from the beginning.我搜索了很多,但找不到读取“HTTP:接受语言”设置“导入”的方法,然后从一开始就为应用程序提供正确的文件。

I know vue.js is a frontend framework but it's served from a node.js server, so there must be a way.我知道 vue.js 是一个前端框架,但它是由 node.js 服务器提供的,所以一定有办法。

Not an expert here, but the only correct long-term solution I see is to do server-side rendering, although this requires some work.不是这里的专家,但我看到的唯一正确的长期解决方案是进行服务器端渲染,尽管这需要一些工作。

As a short-term solution, you could also build all the different versions of your app and serve the correct one, but this is not a very scalable solution.作为一种短期解决方案,您还可以构建应用程序的所有不同版本并提供正确的版本,但这不是一个可扩展的解决方案。

You can get the accepted language by getting the header on the nodejs part.您可以通过获取nodejs部分的标题来获取accepted language

The desired header is req.headers["accept-language"] .所需的标题是req.headers["accept-language"]

Then you need to pass this information on the vue side.那么就需要在vue端传递这些信息。 Depending on how you integrate vue there are different paths to be taken.根据您集成vue方式,可以采用不同的路径。

  • If you are using a template to include vue (pug for instance) you can pass the language as a template variable and include different css files depending on the desired language.如果您使用模板来包含vue (例如 pug),您可以将语言作为模板变量传递,并根据所需的语言包含不同的css文件。
  • If you are including vue using imports then you can do something like this:如果您使用imports包含vue那么您可以执行以下操作:
    • create a module that exports a variable for the language and set the variable to the value you get from the header创建一个模块,为该语言导出一个变量并将该变量设置为您从标题中获得的值
    • on the Vue side import the module and import css based on the value you just setVue端根据你刚刚设置的值导入模块和导入css

I know this is an old post but......我知道这是一个旧帖子,但是......

You can use :您可以使用 :

navigator.language

I returns exactly what you want.我返回的正是你想要的。

Besides that, you can use:除此之外,您还可以使用:

navigator.languages

It returns an array with all browser accepted langs.它返回一个包含所有浏览器接受的语言的数组。 In my case:就我而言:

[ "pt-BR", "pt", "en-US", "en" ]

A complete tutorial (using n18i and vuex) can be found here:完整的教程(使用 n18i 和 vuex)可以在这里找到:

https://itnext.io/setting-language-with-i18n-by-os-language-in-vue-router-vuex-e42c9318c9ec https://itnext.io/setting-language-with-i18n-by-os-language-in-vue-router-vuex-e42c9318c9ec

HTH HTH

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

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