gettext 对于语言文件有什么好处?

发布于 2024-08-04 03:42:23 字数 648 浏览 3 评论 0原文

当谈到用 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 技术交流群。

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

发布评论

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

评论(4

反话 2024-08-11 03:42:23

gettext 的一个好处是它是一种事实上的标准:它被许多应用程序、多种语言所使用——这意味着许多人都在使用它。

另一个好东西(实际上可能是结果,或原因,或两者兼而有之)是一些工具,例如Poedit 例如,它的存在是为了编辑 gettext 文件:您不必浏览一些 PHP(或其他) 源代码。这非常重要:

  • 这意味着非技术人员可以编辑 gettext 本地化文件
    • 也许这对你现在来说似乎并不重要,
    • 但如果您正在开发一个大型开源应用程序,那么它是否成功并且许多人需要以自己的语言提供它就会变得很重要。
  • 这意味着他们不会冒破坏应用程序的风险(例如,由于引号不匹配而导致“解析错误”;-))

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 :

  • it means non-technical people can edit gettext localisation files
    • maybe this doesn't seem important to you now,
    • but if you are working on a big open-source application, it'll become important if it is successful and many people need it in their own language.
  • it means they don't risk to break the application (think "parse error" because of a mismatch in quotes, for instance ;-) )
红颜悴 2024-08-11 03:42:23

您的“实施”存在一些缺点。

  • gettext 不同,它是在 php 中实现的。 ;-)
  • 无论您是否使用它,它都会将整个翻译保留在内存中,
  • 更糟糕的是,每当您需要一行时,它都会实例化整个数据数组(在 PHP 中确实需要一段时间和相当多的内存)
  • 它无法处理多种语言的复数形式
  • 使用此翻译的代码几乎不可读
  • 使用此翻译的代码没有嵌入的后备,可以在缺少翻译的情况下使用。

基本上,您的实现广泛应用于许多项目,这些项目渴望获得国际化支持和高度创新的轮子发明,但不太关心结果,也不知道好坏。

There are a few drawbacks in your "implementation".

  • Unlike gettext it's implemented in php. ;-)
  • It keeps the whole translation in memory no matter if you use it or not
  • Which is worse, it instantiates the whole array of data whenever you need one line (it does take a while and quite some memory in PHP)
  • It's unable to handle plural forms for many languages
  • The code, using this translation is hardly readable
  • The code, using this translation has no embedded fallback to use in case of absent translation.

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.

浅浅 2024-08-11 03:42:23

对于所有类似的问题,答案都是相同的:

什么是更好的[高度证明
和广泛使用的解决方案]
比和[我那狂妄自大的菜鸟
实施]?

  • 不管你相信与否,它已经存在很长时间了,如果经验丰富的开发人员研究过它、使用过它并赞扬过它,那可能是有原因的。
  • 这是一个标准,因此即使您的解决方案更好,打破它所付出的代价也可能不值得。
  • 它可以比您的实现多做 1000 件事,因为它是在全球范围内开发的,而您的想法只是为了解决您的问题。

我不是在批评,我们都这么做了,试图说服自己,我们是聪明人,而其他人是矫枉过正的程序员。这是学习方式的一部分。事实上,我一直在这样做,重新发明轮子,或者向我的朋友/同事炫耀我破解的一些 KISS 代码。随着时间的推移,你最终会越来越少地做这件事,我想你会在你真正值得做的那一天停止这样做:-)

It's the same answer that for all the questions looking like :

What is better with [hightly proved
and vastly used solution]
than with [my hacky cocky noob
implementation]?

  • It's been here for a long time, an believe it or not, if experienced dev worked on it, used it and praised it, it's probably for a reason.
  • It's a standard, and so even if your solution would be better, the price to pay for breaking it may not be worth it.
  • It can do 1000 more than your implementation because it's developed with a global scope in mind whereas your idea only aims to solve your problem.

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 :-)

始终不够爱げ你 2024-08-11 03:42:23

与您的解决方案、Java 字符串表或 Windows 资源等其他解决方案相比,gettext 的优点是它使源代码更具可读性。

比较一下:

printf(_("No photo available (error %d)."), err);

printf(i18n(NO_PHOTO), err);
// or some variant of the same thing

第一种情况下,您可以在代码中看到该消息,并且您确切地知道它的作用。在后一种情况下,您只能看到符号常量,并且必须查找确切的文本和格式说明符。

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:

printf(_("No photo available (error %d)."), err);

with this:

printf(i18n(NO_PHOTO), err);
// or some variant of the same thing

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.

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