如何处理网站上的多种语言

发布于 2024-11-30 09:14:27 字数 766 浏览 1 评论 0原文

我有一个网站,正在翻译成不同的语言。我已翻译内容并将其存储在数据库中。我还在 php 文件中写入了不同的机制,这些机制将根据我在代码中设置的全局定义来显示语言。我对这一切感到高兴。我的问题是如何控制这个全局定义?

我目前有一个 javascript 开关,可以设置 cookie,然后重新加载当前页面。每个后续页面都会读取该 cookie 来设置全局定义。它工作得很好,但是我遇到了两个大问题。 (1)我不能只是无法将网址发送给其中包含该语言的人(我可以执行类似domain.com/forwarder.php?lan=spanish&gotopage=page.php 的操作来设置一个cookie然后转发,但这很丑)。 (2)、搜索引擎无法查看多种语言,因为它们并不真正使用 cookie 和 javascript。

那么我该如何解决这个问题呢?有人有这方面的经验吗?你能分享一下你的经验吗?

我倾向于只使用 url 并删除 cookie;这在我见过的各种国际网站中似乎很受欢迎。所以我猜测网址会是:

domain.com/page       (for english, equivalent to domain.com/en/page)
domain.com/es/page    (for spanish)
domain.com/fr/page    (for french)
etc ......

这是一个好主意吗?我将不得不检查我的代码并在所有 href 前面添加语言代码,这可能会很痛苦。

那么有人对此有何评论吗?这是一个好的计划吗?我是否忽略了意识到一些事情?

I have a website that I am translating into different languages. I have the content translated and stored in a database. I also wrote, into the php files, different mechanisms that will display the language based on a global define I set high in the code. I am happy with all of this. My question is how do I control this global define?

I currently have a javascript toggle that sets a cookie and then reloads the current page. And every subsequent page just reads that cookie to set the global define. It works very well, however I am running into two big problems. (1) I can't just can't have a url to send to somebody that has the language in it (I could do something like domain.com/forwarder.php?lan=spanish&gotopage=page.php that would set a cookie and then forward, but that's ugly). And (2), search engines can't view the multiple languages since they don't really use cookies and javascript.

So how do I solve this? Does anybody have experience in this? Can you share your experiences?

I'm leaning towards just using the url and dropping the cookie; that seems popular among various international sites I've seen. So I'm guessing the urls would be:

domain.com/page       (for english, equivalent to domain.com/en/page)
domain.com/es/page    (for spanish)
domain.com/fr/page    (for french)
etc ......

Is this a good idea? I will have to go through my code and prepend all my href's with the language code, which might be a pain.

So does anybody have any comments on this? Is this a good plan? Am I neglecting to realize something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-12-07 09:14:27

已经很久了,但是不能使用$_SERVER["HTTP_ACCEPT_LANGUAGE"]并自动设置吗?在第一次写入 cookie 之前,用英语或数组中的其他语言在屏幕上留下消息,询问这是否是正确的语言,并下拉可用语言?选择后,将其存储为默认网站语言。

您可以在全局资源文件中使用字符串常量。只有一个网站根据当前语言调用这些字符串常量。

It's been a long time, but can't you use the $_SERVER["HTTP_ACCEPT_LANGUAGE"] and set it automatically. And prior to writing the cookie for the first time, leave message on the screen in either english or another language in the array asking if this is the correct language, with a drop down of available languages? Once it is selected, store that as default website language.

You can use string constants in global resource files. Have only one website that calls those string constants based on the current language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文