翻译算法

发布于 2024-08-07 21:57:47 字数 534 浏览 3 评论 0原文

最近,我正在研究将 PHP Web 应用程序从一种语言翻译成另一种语言的能力。好吧,我读到的大多数内容都涉及语言文件,然后像这样显示所选的文件:

en.lang.php:

<?php

$_TEXT = array();
$_TEXT['welcome'] = 'Welcome to My Application';

?>

fr.lang.php: // french (我用Google翻译的=x)

<?php

$_TEXT = array();
$_TEXT['welcome'] = 'Bienvenue sur mon application Web';

?>

我想问是否有比这更好的工作流程或算法?因为值可能会插入到文本中等等 - 非常棘手的情况。任何帮助谢谢!

另请注意:此应用程序必须跨平台工作(或者我应该说平台独立),因此从 PHP 4.4.2 开始不需要额外的扩展

Recently I am working into the ability of translating a PHP web application from one language to another. Well, most of those I read involves having language files, then display the selected one like this:

en.lang.php:

<?php

$_TEXT = array();
$_TEXT['welcome'] = 'Welcome to My Application';

?>

fr.lang.php: // french (i translated that with Google =x)

<?php

$_TEXT = array();
$_TEXT['welcome'] = 'Bienvenue sur mon application Web';

?>

I would like to ask if there's any better workflow or algorithm than this? Because value may be inserted into the text and so on - pretty hairy situation. Any help thanks!

Also to note that: this application must work cross-platform (or I should say platform-independent) as such no additional extensions are required based from PHP 4.4.2

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

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

发布评论

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

评论(4

苏辞 2024-08-14 21:57:47

我建议使用 gettext,它会让你的生活变得如此精彩更简单...

gettext 函数实现 NLS
(母语支持)API
可用于国际化您的
PHP 应用程序。请参阅
gettext 系统文档
对这些的详细解释
函数或查看文档: »
http://www.gnu.org/software/gettext/manual/gettext。 html.

I would advise using gettext, it'll make your life so much simpler...

The gettext functions implement an NLS
(Native Language Support) API which
can be used to internationalize your
PHP applications. Please see the
gettext documentation for your system
for a thorough explanation of these
functions or view the docs at »
http://www.gnu.org/software/gettext/manual/gettext.html.

深海夜未眠 2024-08-14 21:57:47

查看 PHP 的 gettext: http://www.php.net/manual/en/ intro.gettext.php

如果您想推出自己的简单解决方案,我定义一个函数 __($native, $var1, $var2, ...),它采用本机字符串并执行翻译 +为你进行变量替换。 __(两个下划线)的实际实现取决于您,但通常 $native 是上面数组中的键,它使用 sprintf 来替换。

Checkout PHP's gettext: http://www.php.net/manual/en/intro.gettext.php

If you want to roll your own simple solution, I define a function, __($native, $var1, $var2, ...), that takes the native string and performs the translation + variable substitution for you. The actual implementation of __ (two underscores) will depend on you, but typically $native is the key in your array above and it uses sprintf to substitute.

‘画卷フ 2024-08-14 21:57:47

这实际上称为国际化或本地化。

如果您使用 php,我建议您使用 smarty。您可以执行与使用 Java 相同的操作。 smarty 有 gettext 插件。实际上,您甚至不必搞乱 gettext,您可以比这更容易地做到这一点。
http://www.smarty.net/

或者也许你可以查看我见过的 pear 库包用于本地化。

http://pear.php.net/packages.php?catpid=28

That s actually called Internationalization or localization.

If you are using php, i d recommend you to use smarty. You can do the same thing as you do with Java. and smarty has gettext plugin. you actually dont even have to mess with gettext, you can do it way easier than that.
http://www.smarty.net/

or perhaps you can check out the pear libraries i ve seen packages for localization.

http://pear.php.net/packages.php?catpid=28

小兔几 2024-08-14 21:57:47

正如几位 SOers 同事所建议的那样,gettext 是一个很好的解决方案。它不是特定于 php 的,它非常流行,因此 可以使用多种简化翻译的工具

最后但并非最不重要的一点是,在源代码中保留默认语言的文本比必须记住文本的常量值要容易得多。

gettext, as suggested by several fellow SOers is an excellent solution. It's not php specific, it's quite popular and as such several tools to simplify translation are available.

And last but not least, keeping the text in your default language in the source is many times easier to work with than having to remember the constant values for texts.

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