gettext 对于语言文件有什么好处?
当谈到用 PHP 制作多语言应用程序时,到处都说 gettext 是最好的方法。我只是想知道为什么?
就像下面这个方法相比使用 gettext 效率较低的原因是什么?
<?PHP
//Have seperate language files for each language I add, this would be english file
function lang($phrase){
static $lang = array(
'NO_PHOTO' => 'No photo\'s available',
'NEW_MEMBER' => 'This user is new'
);
return $lang[$phrase];
}
//Then in application where there is text from the site and not from users I would do something like this
echo lang('NO_PHOTO'); // No photo's available would show here
?>
Everywhere on SO when it comes to making a multi language app in PHP everyone say that gettext is the best way to do it. I am just wanting to know why?
Like what makes this method below, less efficient then using gettext?
<?PHP
//Have seperate language files for each language I add, this would be english file
function lang($phrase){
static $lang = array(
'NO_PHOTO' => 'No photo\'s available',
'NEW_MEMBER' => 'This user is new'
);
return $lang[$phrase];
}
//Then in application where there is text from the site and not from users I would do something like this
echo lang('NO_PHOTO'); // No photo's available would show here
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
gettext 的一个好处是它是一种事实上的标准:它被许多应用程序、多种语言所使用——这意味着许多人都在使用它。
另一个好东西(实际上可能是结果,或原因,或两者兼而有之)是一些工具,例如Poedit 例如,它的存在是为了编辑 gettext 文件:您不必浏览一些 PHP(或其他) 源代码。这非常重要:
A good thing with gettext is that it is kind of a de-facto standard : it is used by lots of applications, in many languages -- which means many people work with it.
Another good thing (maybe a consequence, or a cause, or both, actually) is that several tools, like Poedit for instance, exist to edit gettext files : you don't have to go through some PHP (or whatever) source-code. And this is very important :
您的“实施”存在一些缺点。
gettext
不同,它是在php
中实现的。 ;-)基本上,您的实现广泛应用于许多项目,这些项目渴望获得国际化支持和高度创新的轮子发明,但不太关心结果,也不知道好坏。
There are a few drawbacks in your "implementation".
gettext
it's implemented inphp
. ;-)Basically, your implementation is widely used in many projects who are dying to claim their internationalization support and highly innovative invention of the wheel, but don't care a bit about the result and do not know the good from the bad.
对于所有类似的问题,答案都是相同的:
我不是在批评,我们都这么做了,试图说服自己,我们是聪明人,而其他人是矫枉过正的程序员。这是学习方式的一部分。事实上,我一直在这样做,重新发明轮子,或者向我的朋友/同事炫耀我破解的一些 KISS 代码。随着时间的推移,你最终会越来越少地做这件事,我想你会在你真正值得做的那一天停止这样做:-)
It's the same answer that for all the questions looking like :
I am not criticizing, we all did that, trying to convince ourself that we are smarties and others are overkill programmers. That's part of the way to learn. I actually continuously do that, reinventing the wheel or showing off with my friends / colleagues about some KISS code I hacked. With time, you just end up doing it less and less, and I suppose you stop doing it the day when you would actually deserve to do it :-)
与您的解决方案、Java 字符串表或 Windows 资源等其他解决方案相比,gettext 的优点是它使源代码更具可读性。
比较一下:
在
第一种情况下,您可以在代码中看到该消息,并且您确切地知道它的作用。在后一种情况下,您只能看到符号常量,并且必须查找确切的文本和格式说明符。
The advantage of gettext, compared to other solutions like yours, Java string tables or Windows resources, is that it makes the source code more readable.
Compare this:
with this:
In the first case, you can see the message right there in the code and you know exactly what it does. In the latter case, you only see a symbolic constant and have to look up the exact text and format specifiers.