Web 应用程序中的一些字符串的翻译。

发布于 2024-10-09 16:28:22 字数 53 浏览 1 评论 0原文

当用户想要以他的语言查看这些字符串时,如何将 Web 应用程序中的一些字符串翻译为其他语言?

How to translate a few strings in a web application to a different language when the user wants to see those strings in his language?

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

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

发布评论

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

评论(1

缘字诀 2024-10-16 16:28:22

你熟悉 i18n 吗?它是“internationalization”的缩写,是一个 20 个字母的单词:第一个“i”,然后是 18 个字母,然后是“n”:D

有些人喜欢为此使用属性文件。您可以根据 i18n 语言名称来命名属性文件。因此,对于英语和瑞典语文本,您将拥有例如 en-us.propertiessw-se.properties 。在这些文件中,您将拥有一些要显示的文本的占位符名称,以及它们的实际值。请记住,占位符不能有空格!

en-us.properties 将包含

welcome_message = "welcome %user%\nplease check your weapons at the door :)"
logout_button_text = "log out"
good_bye_message = "godspeed to you %user%!"

sw-se.properties 将包含

welcome_message = "välkommen %user%\nvänligen lämna dina vapen vid dörren :)"
logout_button_text = "logga ut"
good_bye_message = "%user%, vi önskar dig en lycklig resa!"

我已经编写了此功能,其中 % 符号内的内容是占位符,将在语言文本已被保留,以便会话或数据库中的数据可以放置在消息中。

请注意语言文件如何包含完全相同的键,但具有不同的值。

当您想要显示文本时,

  • 您检查您使用的语言(可能来自网址),
  • 加载相应的属性文件,
  • 获取给定点所需的概念占位符的文本,
  • 替换任何数据占位符在此字符串中,
  • 并将最终结果推送给用户。

are you familiar with i18n? it's short for "internationalization" which is a 20 letter word: first "i", then 18 letters, and then "n" :D

some people like to use properties-files for this. you name the property-file after its i18n language name. so you would have for example en-us.properties and sw-se.properties for english vs swedish texts. in these files you would then have place holder names for some texts you want to display, and what their actual values would be. remember that place holders can't have spaces!

en-us.properties would contain

welcome_message = "welcome %user%\nplease check your weapons at the door :)"
logout_button_text = "log out"
good_bye_message = "godspeed to you %user%!"

sw-se.properties would contain

welcome_message = "välkommen %user%\nvänligen lämna dina vapen vid dörren :)"
logout_button_text = "logga ut"
good_bye_message = "%user%, vi önskar dig en lycklig resa!"

i've made up this functionality where things within % signs are placeholders to be replaced after the language text has been retained, so that data from the session or the database can be placed within the message.

note how the language files both contain the exact same keys, but with different values.

when you want to display text,

  • you check what language you're in (maybe this comes from the url),
  • load the corresponding properties file,
  • get the text for the conceptual place holder that's needed at the given point,
  • replace any data place holders within this string,
  • and push the final result out to the user.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文