使用PHP的Gettext无需安装语言环境 [英] Use PHP Gettext without having to install locales

查看:146
本文介绍了使用PHP的Gettext无需安装语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看选项,国际化的一个开源项目:

I've been looking at options for internationalizing an open source project:


  • Gettext的,这似乎每个人都建议,显然需要区域设置为安装在系统上来使用。看到这个注意在PHP手册,这正好反映我的情况。这 SO质疑还询问了同样的问题。因为我不能相信最终用户拥有其系统上安装相应的语言环境,这是不适合的开源项目。此外,这是非常非常奇怪的,你必须只安装了语言环境使用已翻译的字符串(IMO)。

  • Gettext, which everyone seems to recommend, apparently needs locales to be "installed on your system" to be used. See this note on the PHP manual, which reflects exactly my situation. This SO question also asks about the same problem. This isn't suitable for an open source project because I can't trust that the end users have the appropriate locales installed on their system. Besides, it's very very weird that you have to install locales just to use strings that you've translated (IMO).

一个Zend_Translate还建议支持的gettext的时候,但是我没有使用Zend框架,所以我不认为这是我的选择。有些人说,你可以从Zend框架拆分它,但我不知道怎么样。如果有人可以告诉我,我需要哪些文件(我已经下载了Zend Framework的压缩包),挑,我会开到里用Zend_Translate。

Zend_Translate is also recommended in favour of gettext sometimes, but I'm not using the Zend framework so I don't think it's an option for me. Some people have said you can split it from the Zend framework, but I'd have no clue how. If anyone can tell me what files I need (I've downloaded a tarball of the Zend framework) to pick out, I would be open to using Zend_Translate.

阵列。这就是我现在做的事情,但它不是理想的,因为:

Arrays. That's what I'm doing now, but it's not ideal because:


  • 这将是使用了大量的内存来定义每一个翻译的时候,大部分不会受到当前页面中使用。

  • 我有发现自己复制阵列,这已经成为1000行code长的钥匙的问题,我很少添加任何东西...

  • 这意味着非程序员不能真正转化,而poEdit的是每个人都希望使用标准。

我可以以某种方式读的.mo 文件而不Gettext的或一个Zend_Translate,还是必须使用gettext?如果是这样,我怎么可以让所有的语言环境里,比如在这个问题我联系上面?

Can I somehow read .mo files without Gettext or Zend_Translate, or must I use Gettext? If so, how can I make all locales work, like in the question I linked to above?

编辑:现在我愿意用一个Zend_Translate。我只需要找出哪些文件,我需要(这将是巨大的,如果他们能合并成一个文件) - 我不想整个Zend框架在我的项目

I'm now willing to use Zend_Translate. I just need to work out what files I need (it would be great if they could be combined into one file) - I don't want the entire Zend Framework in my project.

更新:我很感兴趣,看看源有多大打开的项目处理国际化:

Update: I was interested to see how big open source projects handle i18n:


  • 的Moodle使用阵列

  • 的Drupal似乎使用阵列,与数据库回退。他们还运行的setlocale('C')来得到一个通用的语言环境和规避整个的setlocale 一塌糊涂。

  • 字preSS使用<一个href=\"https://github.com/Word$p$pss/Word$p$pss/blob/55fb020e84fd46bd56668facd1f3a74be1776cbf/wp-includes/l10n.php\"相对=nofollow>的自身职能堆了解国际化,虽然底下确实使用gettext。

  • Moodle uses arrays.
  • Drupal seems to use arrays, with a database fallback. They also run setlocale('C') to get a generic locale and circumvent the entire setlocale mess.
  • Wordpress uses heaps of their own functions for i18n, although underneath does use gettext.

所以没有这三个随机项目都使用了Zend_Framework,也不直接gettext的,据我所看到的。

So none of those three random projects use Zend_Translate, nor gettext directly, as far as I can see.

也许是使用 C 区域,存储在文本域名语言的名称,并从那里走个好主意。

Maybe it's a good idea to use the C locale, store the name of the language in the text domain name, and go from there.

所以这里就我与有:

$lang = 'de'; //debug
setlocale( LC_ALL, 'C' );
bindtextdomain( 'default', PATH . "/locale/$lang" );
bind_textdomain_codeset( 'default', 'UTF-8' );
textdomain( 'default' );

var_dump( file_exists( PATH . "/locale/$lang/C/LC_MESSAGES/default.mo" ) ); //bool(true)

不过我还是刚刚得到的英文字符串,即使我用poEdit的,的msgfmt等方面做出相应的文件。我也试着重新启动Apache的。

But I still just get the English string, even though I've used poedit, msgfmt etc. to make the appropriate files. I've also tried restarting Apache.

推荐答案

下面是解决方案:

$lang = 'de'; //debug
setlocale( LC_ALL, 'C.UTF-8' );
bindtextdomain( 'default', PATH . "/locale/$lang" );
bind_textdomain_codeset( 'default', 'UTF-8' );
textdomain( 'default' );

这和我张贴在我的答案底部的例子之间的唯一区别是,它采用 C.UTF-8 而不仅仅是Ç

The only difference between that and the example I posted at the bottom of my answer is that it uses C.UTF-8 not just C.

我会做这个更多的测试,如果它跨平台工作,如果我发现任何东西都不会更新这个答案。

I'll be doing more testing of this, and if it works cross-platform, and will update this answer if I find out anything else.

这篇关于使用PHP的Gettext无需安装语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆