制作多语言网站的最佳方法是什么?

时间:2018-07-24 10:00:28

标签: javascript php html server multilingual

我需要用两种语言制作登录页面,通过单击标志来交换语言。

我的理解是,我需要将语言状态保存在用户的本地存储/ cookie中。

我在这里看到3个选项:

  • 在用户计算机.json上上载两种语言的内容。在页面打开时,脚本将检查localstorage以获取lang变量的可用性,并在由JS填充页面之后。

  • 将lang变量保存在cookie中,并且对于服务器来说,服务器必须具有魔力,并发送基于用户语言的页面。

  • 以每种语言复制每个页面。

问题1:
哪种方法更好,何时以及为什么? (案例:一页登陆,站点多页)

问题2:
复制页面时,在根目录中根本没有index.html是正常的,但是:

/en/index.html  
/ru/index.html  

.htaccess文件默认情况下重定向到/en/,检查cookie中是否有lang变量,如果存在,则将用户重定向到特定文件夹。

2 个答案:

答案 0 :(得分:1)

I would store each one of translations in a json file (en.json, ru.json) and change languages using url parameters. index.html?lang=ru (This way you don't have to handle different paths)

If the url parameter exists, save it on localstorage, load the respective json and fill the page with the tanslated strings. If there is no url parameter, look in the localstorage for a language. If there is nothing there, assume a default (en, for example) and load the translated strings.

答案 1 :(得分:1)

Question 1: Option 1: Good practice if you have some sort of frontend framework (like Angular or React). But if you have a static (or only serverside rendering) page it is not really the way to go. Because it add unnecessary complexity to your code.

Option 2: Good way to do it if you only have backend rendering (for example with PHP).

Option 3: Do this only if you have static webpage (only HTML/CSS) which does not change. And you don't have programming experience at all.

Question 2: Well no you always have a index.html but you can redirect to the index.html of the default language.