I have heard that PHP6 will natively support unicode, which will hopefully make multi-language support much easier. However, PHP5 has pretty weak support for unicode and multi-language (i.e. just a bunch of specialized string functions).
我听说过PHP6本身会支持unicode,希望能够更轻松地支持多语言。但是,PHP5对unicode和多语言(即只是一堆专门的字符串函数)的支持非常弱。
I was wondering what are your strategies to enable unicode and multi-languaage support in your PHP5 applications?
我想知道你在PHP5应用程序中启用unicode和多语言支持的策略是什么?
Also, how do you store translations since PHP5 doesn't have WebResource file like ASP.NET does?
另外,你如何存储翻译,因为PHP5没有像ASP.NET这样的WebResource文件呢?
4 个解决方案
#1
It's not all that hard really, but you may want to make your question a bit more specific.
这真的不是那么难,但你可能想让你的问题更具体一些。
If you're talking to a database, make sure your database stores data in UTF-8 and the connection to your database is in UTF-8 (a common pitfall). Make sure to run this when establishing a connection:
如果您正在与数据库通信,请确保您的数据库以UTF-8存储数据,并且与数据库的连接是UTF-8(一个常见的陷阱)。确保在建立连接时运行此命令:
mysql_set_charset('utf8');
For user input, set the accept-charset
attribute on your forms.
对于用户输入,请在表单上设置accept-charset属性。
<form accept-charset="utf-8">
Serve your sites with an appropriate HTTP header:
使用适当的HTTP标头为您的网站提供服务:
header('Content-Type: text/html; charset=utf-8');
or at least set appropriate meta tags for your site:
或者至少为您的网站设置适当的元标记:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Keep your source code files encoded in UTF-8.
保持源代码文件以UTF-8编码。
If you keep everything in UTF-8, you usually don't need to worry about anything. It's only getting problematic once you start mixing encodings throughout your app.
如果你把所有东西都保存在UTF-8中,你通常不需要担心任何事情。一旦你开始在整个应用程序中混合编码,它就会出现问题。
If you're starting to talk about string manipulation of course, you'll have to take a little more care. Mostly you'll want to use the mb_
set of string functions, as you pointed out yourself.
如果你开始谈论字符串操作当然,你将不得不采取更多的关注。大多数情况下,你会想要使用mb_字符串函数集,正如你自己指出的那样。
#2
Well for app development in PHP I use CodeIgniter which takes care of handling multiple language files. It's very powerful and easy to use.
对于PHP中的应用程序开发,我使用CodeIgniter来处理多个语言文件。它非常强大且易于使用。
Here is a link to their Language Class
这是他们的语言课程的链接
#3
For translations, you can either use a framework, or just roll your own library. You can store translations in csv files and use PHP's fgetcsv() to parse it. CSV files can be edited with any spreadsheet app.
对于翻译,您可以使用框架,也可以只滚动自己的库。您可以将翻译存储在csv文件中,并使用PHP的fgetcsv()来解析它。可以使用任何电子表格应用编辑CSV文件。
For an example, you can look at the code of Zend_Translate (part of Zend Framework). It's easy to follow along.
例如,您可以查看Zend_Translate(Zend Framework的一部分)的代码。它很容易跟随。
#4
Related to usage of mb_* set of functions, at the same time of maintaining compatibility, see the mb_string.overload
php.ini directive.
与使用mb_ *函数集相关,在保持兼容性的同时,请参阅mb_string.overload php.ini指令。
It will allow you to use the regular string functions which have been overloaded by the multi-byte enabled ones.
它允许您使用已启用多字节的常规字符串函数。
#1
It's not all that hard really, but you may want to make your question a bit more specific.
这真的不是那么难,但你可能想让你的问题更具体一些。
If you're talking to a database, make sure your database stores data in UTF-8 and the connection to your database is in UTF-8 (a common pitfall). Make sure to run this when establishing a connection:
如果您正在与数据库通信,请确保您的数据库以UTF-8存储数据,并且与数据库的连接是UTF-8(一个常见的陷阱)。确保在建立连接时运行此命令:
mysql_set_charset('utf8');
For user input, set the accept-charset
attribute on your forms.
对于用户输入,请在表单上设置accept-charset属性。
<form accept-charset="utf-8">
Serve your sites with an appropriate HTTP header:
使用适当的HTTP标头为您的网站提供服务:
header('Content-Type: text/html; charset=utf-8');
or at least set appropriate meta tags for your site:
或者至少为您的网站设置适当的元标记:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Keep your source code files encoded in UTF-8.
保持源代码文件以UTF-8编码。
If you keep everything in UTF-8, you usually don't need to worry about anything. It's only getting problematic once you start mixing encodings throughout your app.
如果你把所有东西都保存在UTF-8中,你通常不需要担心任何事情。一旦你开始在整个应用程序中混合编码,它就会出现问题。
If you're starting to talk about string manipulation of course, you'll have to take a little more care. Mostly you'll want to use the mb_
set of string functions, as you pointed out yourself.
如果你开始谈论字符串操作当然,你将不得不采取更多的关注。大多数情况下,你会想要使用mb_字符串函数集,正如你自己指出的那样。
#2
Well for app development in PHP I use CodeIgniter which takes care of handling multiple language files. It's very powerful and easy to use.
对于PHP中的应用程序开发,我使用CodeIgniter来处理多个语言文件。它非常强大且易于使用。
Here is a link to their Language Class
这是他们的语言课程的链接
#3
For translations, you can either use a framework, or just roll your own library. You can store translations in csv files and use PHP's fgetcsv() to parse it. CSV files can be edited with any spreadsheet app.
对于翻译,您可以使用框架,也可以只滚动自己的库。您可以将翻译存储在csv文件中,并使用PHP的fgetcsv()来解析它。可以使用任何电子表格应用编辑CSV文件。
For an example, you can look at the code of Zend_Translate (part of Zend Framework). It's easy to follow along.
例如,您可以查看Zend_Translate(Zend Framework的一部分)的代码。它很容易跟随。
#4
Related to usage of mb_* set of functions, at the same time of maintaining compatibility, see the mb_string.overload
php.ini directive.
与使用mb_ *函数集相关,在保持兼容性的同时,请参阅mb_string.overload php.ini指令。
It will allow you to use the regular string functions which have been overloaded by the multi-byte enabled ones.
它允许您使用已启用多字节的常规字符串函数。