php gettext 多语言翻译

1、在window与linux下的多语言切换有些区别,主要putenv的设置区别。

  参考链接:http://www.cnblogs.com/sink_cup/archive/2013/11/20/ubuntu-php-gettext-error.html

       http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/08/09/2132603.html

  

    在window下echo setlocale('LANG=zh_CN'),而在linux下如果支持该语言,则会输出设置的内容

     windows的php代码:

     

<?php
// translate();
// include_once 'include/setLan.php';
echo putenv('LANG=zh_CN') . "--------------";

echo setlocale(LC_ALL, 'zh_CN');

$domain = 'translate'; //域名,可以任意取个有意义的名字,不过要跟相应的.mo文件的文件名相同(不包括扩展名)。
echo bindtextdomain("translate", dirname(__FILE__) . "/locale/") . "-------"; //设置某个域的mo文件路径
echo bind_textdomain_codeset("translate", 'UTF-8') . "-------------"; //设置mo文件的编码为UTF-8
// echo $domain;
echo textdomain($domain) . "----------------------<br>"; //设置gettext()函数从哪个域去找mo文件

$str = _("hello world love you");
print <<<HTML
<a href="#">{$str}</a>
HTML;
?>
<html>
<head>
<meta http-equiv="Content-Type"  content= "text/html; charset=utf-8"  />
<title>title</title>
</head>
<body bgcolor="#FFFFFF"  text= "#000000"  link= "#FF9966"  vlink= "#FF9966"  alink= "#FFCC99" >
<?=gettext('hello world love you')?>
</body>
</html>

 

linux下:

<?php
$lang = 'en_US'; //linux不支持zh-Hans,所以gettext、php都不支持。使用locale -a可以看到os支持的语言文字国家地区码。
putenv('LANGUAGE=' . $lang); //ubuntu 需要,centos不需要此行
echo setlocale(LC_ALL, $lang . '.utf8'); //ubuntu 需要.utf8,centos可有可无
$domain = 'translate';
bindtextdomain($domain, dirname(__FILE__) . '/locale');
bind_textdomain_codeset($domain , 'UTF-8');
textdomain($domain);

echo _('asdf');
echo "\n";
exit;
?>

 目录为en_US,如下:

|  |---locale
|  |-----en_US
|  |-----LC_MESSAGES
|  |       |----------translate.mo translate.po
|  |        

 

桌面linux:要实现中文的翻译,需要设置linux的区域语言。设置方法:

        http://blog.sina.com.cn/s/blog_49f914ab0100sbsp.html

linux 区域语言的工作原理

   linux中的多语言环境(LC_ALL, LANG, locale) 

   

开发板要支持国际化(Internationalization,简写为I18N,需要加入以下操作

1、busybox支持中文的显示,主要是加入locale的支持:

  http://blog.csdn.net/lqx4_3/article/details/7281034

 

2、开发板下存在/usr/lib/locale/locale-archive的编码集。生成locale-archive方法。 busybox没有locale相关命令,可以在桌面上linux上生成拷贝到开发板上。

     生成编码集:    http://blog.chinaunix.net/uid-20648944-id-2950413.html

posted @ 2016-10-20 10:01  cogitoergosum  阅读(1338)  评论(0编辑  收藏  举报