在软件或网站中切换语言的最佳方法?

发布于 2024-12-29 05:55:56 字数 47 浏览 1 评论 0原文

我想知道在软件或网站中切换语言的最佳编程技术。那么有人可以给我一些建议吗?谢谢!

I want to know the best programming technique to switch languages in the software or website. So anyone can give me some suggestions? Thanks!

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

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

发布评论

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

评论(2

成熟稳重的好男人 2025-01-05 05:55:56

没有标准。但我在网站中使用了以下技术。

  1. 将所有可见字符串(例如“文件”、“菜单”、“联系我们”)放入文件或数据库中。它可以是 xml、json 或任何您稍后可以轻松解析的格式。确保根据区域设置/语言将所有数据保存在不同的目录/数据库表中。
  2. 使用函数或静态类方法包装所有字符串。每次显示它们时,它们都会经过一些处理。
  3. 当您首次加载软件或网络时,确定用户想要使用的区域设置或语言。
  4. 根据用户的选择选择字符串源。例如,如果用户想要查看 en_GB 语言环境中的字符串,请在 en_GB 文件夹或 lang_en_GB 表中查找字符串。命名格式由您掌控。
  5. 现在,每次调用函数或静态类方法时,都会在您在第 4 步中选择的源中查找字符串。

There is no standard. But I used the following technique in websites.

  1. Put all the visible strings (eg. "file", "menu", "contact us") in a file or database. It can be xml, json or any format that you easily parse later. Make sure all the data is saved in different directory/db tables according to locale/language.
  2. Wrap all the strings with a function or static class method. Every time they are displayed they'll go through some processing.
  3. When you first load the software or web determine the locale or language that user wants to use.
  4. According to users choice select the string source. database, filepath etc. For e.g. if user want to see strings in en_GB locale, find strings in en_GB folder or in lang_en_GB table. Naming format is in your hand.
  5. Now each time the function or static class method is called find strings in the source you selected on Step 4.
审判长 2025-01-05 05:55:56

这取决于您使用的语言。许多人都对此表示支持。例如,在 android 中,您所做的是将所有字符串存储到根据区域命名的 xml 文件中。并且操作系统会根据手机区域自动使用适当的资源。

查看您的语言/平台包含哪些支持。您需要将所有字符串常量(例如“按菜单按钮”)外部化到外部文件中。然后只需将此文件翻译为您想要支持的每种语言,并使用操作系统自动使用正确的资源文件。或者,如果平台/操作系统不支持这样的事情,请编写一些代码来确定自己使用哪个资源文件。

It depends on the language you are using. Many have built in support for it. In android for example what you do is store all strings into an xml file that is named based on the region. And the operating system will automatically use the appropriate resources based on the phones region.

Look into what support your language/platform includes. You will want to externalize all your string constants such as "Press the menu button" into an external file. Then just get this file translated to each language you want to support, and use the OS to automatically use the correct resource file. Or if the platform/OS does not support such a thing write some code to determine which resource file to use yourself.

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