如何开发一个易于本地化的webapp系统?

发布于 2024-12-09 10:49:18 字数 337 浏览 0 评论 0原文

我正在构建一些网络应用程序,我正在考虑为其添加一些本地化,以便人们可以轻松地在语言之间切换,也许在未来 - 能够为应用程序添加他们自己的本地化,就像 Facebook 的一样语言或用户声音。

我知道我应该有一个模板,例如:

<element>[string1]</element>

基本上, [string1] 将被替换为正确的字符串,但是我将它存储在哪里?数据库?静态文件?每次从数据库获取字符串数据不是“昂贵”吗?我该如何处理它?使用输出缓冲区,或者是否有一些我不知道的更好的方法。

如果有人能以正确的思维方式指导我,我将不胜感激。

I am in the midst of building some web-app, and I am thinking of adding some localization to it, so people could switch easily between languages, and perhaps in the future - be able to add their own localization for the app, like Facebook's languages or uservoice.

I get it that I should have a templates, something like:

<element>[string1]</element>

and basically, that [string1] will be replaces with the correct string, but where do I store it? database? static file? isn't it "expensive" to get the string's data every time from the database? and how do I process it? using output buffer, or is there some better method that I am not aware of.

If anyone could direct me in the correct way of thinking, I will appreciate it.

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

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

发布评论

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

评论(5

孤檠 2024-12-16 10:49:18

有许多可用的开源翻译套件系统可以让您更好地理解该做和不该做的事情(例如potttlepoeditvirtaal)。

如果您决定走自己的路 - 我建议将所有翻译键和值存储在数据库中。当涉及到编辑、搜索、获取缺失的翻译等时,您的情况会更好。

您可以每 24 小时或在每个新/编辑的翻译之后生成完整的翻译列表,然后将其存储在 JSON 或 PHP 序列化的静态文件中格式。这将使您免于 MYSQL 选择。

我建议的另一件事是选择“密钥+密钥翻译”的格式。应在第一次调用时将 KEYS 插入数据库以获取翻译。这将导致您看到“缺少翻译”,这是使用第三方或您的网站访问者进行本地化翻译的好方法。

There is a number of open-source translation kit systems available which should give you a better understaning of the DOs and DON'Ts (ex. pottle, poedit, virtaal).

If you decide to go your way - I'd suggest storing all the translation keys and values on the database. You'll be better off when it comes to editing, searching, getting missing translations etc.

You can generate full list of translations every 24 hours or after each new/edited translation, which would then be stored on static file in JSON or PHP serialised format. This will save you from MYSQL selects.

Another thing I would suggest is choosing a format of KEYS + KEY TRANSLATIONS. KEYS should be inserted to database on the first call to get the translation. This will lead you to "Missing translations" which is a good way of using 3rd parties or your site visitors to do the localised translations.

爱给你人给你 2024-12-16 10:49:18

当我必须本地化我的应用程序时,我通常会创建一个 translations/ 目录,然后为每种区域性创建一个子目录。例如,我可以在其中包含以下文件夹:

  • en-US/ - 包含美国用户的翻译。
  • en-GB/ - 包含英国用户的翻译。
  • en/ - 包含后备通用英语翻译。

然后我以这种方式获取区域设置:

<?php
$default_locale = 'en';

$parts     = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$sub_parts = explode('-', $parts[0]);

if (is_dir("languages/{$parts[0]}")) {
    $locale = $parts[0];
}
elseif (is_dir("languages/{$sub_parts[0]}")) {
    $locale = $sub_parts[0];
}
else {
    $locale = $default_locale;
}

请注意,您应该对 $_SERVER['HTTP_ACCEPT_LANGUAGE'] 变量执行检查,以避免 LFI(本地文件包含)尝试。只需检查语言环境是否包含在 languages/ 目录中即可。

When I have to localize my application, I usually create a translations/ directory, and then a subdirectory for each culture. For example, I can have the following folders inside:

  • en-US/ - Contains the translations for users from the USA.
  • en-GB/ - Contains the translations for users from Great Britain.
  • en/ - Contains fallback, generic english translations.

Then I get the locale this way:

<?php
$default_locale = 'en';

$parts     = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$sub_parts = explode('-', $parts[0]);

if (is_dir("languages/{$parts[0]}")) {
    $locale = $parts[0];
}
elseif (is_dir("languages/{$sub_parts[0]}")) {
    $locale = $sub_parts[0];
}
else {
    $locale = $default_locale;
}

Note that you should perform a check on the $_SERVER['HTTP_ACCEPT_LANGUAGE'] variable to avoid LFI (Local File Inclusion) attempts. Just check the locale is contained in the languages/ directory.

2024-12-16 10:49:18

在 PHP 中执行此操作的常用方法是将所有字符串存储在 PHP 文件内的 PHP 数组中。

The usual way to do this in PHP is by storing all your strings in a PHP array, inside a PHP file.

夏有森光若流苏 2024-12-16 10:49:18

我建议你看一下 Gettext for PHP。 gettext 函数实现了 NLS(本机语言支持)API,可用于国际化 PHP 应用程序。

Gettext 是 Linux/Unix 系统本地化的事实上的标准,并且也广泛用于 PHP 应用程序(例如,Wordpress 就使用它)。如需更深入的解释、示例等,请查看上面 PHP 手册中的 Gettext 部分,或者您可以访问 原始 gettext 手册,对 gettext 工具、绑定等进行更广泛的讨论。

I suggest you take a look at Gettext for PHP. The gettext functions implement an NLS (Native Language Support) API which can be used to internationalize your PHP applications.

Gettext is a de-facto standard for localization on linux/unix systems, and is widely used in PHP applications too (Wordpress, for example, uses it). For more in-depth explanation, examples, etc., take a look at the Gettext section in the PHP manual above, or you can visit the original gettext manual for a broader discussion of gettext tools, bindings, and more.

梦里的微风 2024-12-16 10:49:18

Wordpress 使用 gettext 库,可能这对您有用。

Wordpress uses gettext library, may be this would be useful to you.

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