复制()函数PHP,设置charset?

时间:2011-11-28 19:19:43

标签: php character-encoding gettext

我在PHP中使用gettext,并且由于.mo文件的缓存,我有一个小脚本,复制原始的.mo文件,并将时间附加到它,因此它被重新加载。问题是,使用copy()函数,似乎破坏了文件的字符集?

因为我有丹麦语翻译,UTF-8支持丹麦字符 - 原始文件运行UTF-8,但在复制之后,它似乎不能用于某种原因。

 <?php
//Starting a session, to store the choosen langauge
session_start();

if (isset($_GET['lang']))
{
    //If the $_GET langauge is set, set the $locale to the get request.
    $locale = $_GET["lang"];
}
else if (isset($_SESSION["lang"]))
{
    //else if the session['lang'] is set, then set the $locale to that session
    $locale = $_SESSION["lang"];
}
else
{
    //Else default is english
    $locale = "en_US";
}

$_SESSION["lang"] = $locale;

$locales_root = "Locale";  // locales directory
$domain = "messages"; // the domain you're using, this is the .PO/.MO file name without the extension

// activate the locale setting
setlocale(LC_ALL, $locale);
setlocale(LC_TIME, $locale);
putenv("LANG=$locale");
// path to the .MO file that we should monitor
$filename = "$locales_root/$locale/LC_MESSAGES/$domain.mo";
$mtime = date('d.H.i.s', strtotime(filemtime($filename))); // check its modification time
// our new unique .MO file
$filename_new = "$locales_root/$locale/LC_MESSAGES/{$domain}_{$mtime}.mo"; 

if (!file_exists($filename_new)) {  // check if we have created it before
      // if not, create it now, by copying the original
      copy($filename,$filename_new);
}
// compute the new domain name
$domain_new = "{$domain}_{$mtime}";
// bind it
bindtextdomain($domain_new,$locales_root);
// then activate it
textdomain($domain_new);
// all done
?>

2 个答案:

答案 0 :(得分:0)

不确定是否有帮助,但您是否尝试先设置以下任何一项?

mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
ob_start('mb_output_handler');

修改

您也可以使用copy()中的第3个参数 - 请参阅文档http://uk.php.net/copy - 可以使用stream_context_create()将标头设置为UTF-8

答案 1 :(得分:0)

我不认为copy()应该做任何神奇的事情,它应该只做一个二进制副本。除非可能有一些设置在php的流功能中运行...

- 比较文件内容以检查

printf("%s\n%s", md5_file($filename), md5_file($filename_new));