翻译算法
最近,我正在研究将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议使用 gettext,它会让你的生活变得如此精彩更简单...
I would advise using gettext, it'll make your life so much simpler...
查看 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.
这实际上称为国际化或本地化。
如果您使用 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
正如几位 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.