PHP, MultiLanguage, PO MO gettext() 多國語言版本

PHP, MultiLanguage, PO MO gettext() 多國語言版本

在php 要如何做多國語言支援? 下面的步驟可以說明

請把下面的php code 存起來

#. tset.php


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<?
$domain = 'messages';
bindtextdomain($domain, "includes/translations");
textdomain($domain);

$iso_codes = array (
'EN'=>'English',
'fr'=>'French',
'it'=>'Italian',
'pt'=>'Portuguese',
'es'=>'Spanish',
'zh'=>'Chinese','nl'=>'Nederlands'
);
foreach ($iso_codes as $iso_code => $language) {
putenv ('LANG='.$iso_code);      //Print out the language name and greeting
setlocale(LC_ALL, $iso_code);    //Filter the greeting through gettext
echo $iso_code;
printf ("%12s: %s\n", $language, gettext("str_hello")) &amp;amp; "\n";
echo gettext("txt");
echo gettext("str_hello");
echo gettext("URL");
echo gettext("Row");
echo  '<br>';
}
>

<?php
putenv("LANG=nl_NL");
setlocale('LC_ALL', "nl_NL");
bindtextdomain("greetings", "./locale/");
textdomain("greetings");

echo _("Hello World");
>
<body>
</body>
</html>

#. 然後下載工具 Poedit

http://sourceforge.net/projects/poedit/

$./xgettext -n *.php

如果是中文的 _("中文") 就要用

$ ./xgettext --from-code=UTF-8 -n  *.php

$>ls
tset.php messages.po

#. Use Poedit to edit .po file and convert string, and save to .mo (msgfmt.exe)
#. COPY .po and .mo  \includes\translations\zh\LC_MESSAGES\messages.po and messages.po

PHP
01 StringCH02 Array技術文章CodeIgniter 技術文章youtube api 技術文章新浪微博开放平台