多语言PHP应用程序:最佳实践? [英] Multi language PHP application: best practice?

查看:78
本文介绍了多语言PHP应用程序:最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想就我在PHP MVC Web应用程序上实现多语言支持的方式获得您的反馈.我就是这样的:

I'd like to get your feedback over the way I implemented multi-language support on my PHP MVC web app. That's how I did:

  • 在/app文件夹中,我创建了一个/languages文件夹,其中每种语言包含一个文件(english.php,spanish.php等).
  • 每个文件包含一系列在每个文件中具有相同名称的变量,其中包含要在视图中呈现的文本
  • 然后这些变量在不同的视图中回显
  • 当用户更改语言时,将更新语言" cookie变量
  • 在每个视图的控制器中,我包括以下格式的语言文件:

包括$ _SERVER ['DOCUMENT_ROOT']. "/app/languages/". $ _COOKIE ["language"]. ".php";

include $_SERVER['DOCUMENT_ROOT'] . "/app/languages/" . $_COOKIE["language"] . ".php";

对我来说听起来很整洁.您会对这种方法有否定的想法吗?

Sounds pretty neat to me. Would you have any negative thoughts about this method?

推荐答案

在使用PHP 5.3.2时,我绝对建议您查看 intl 扩展名(如果可以).它充满了方便的功能和类,可以协助i18n.例如,您可以使用以下代码段从可用的访问者中为访问者生成最适合"的语言:

As you're on PHP 5.3.2 I'd definitely recommend checking out the intl extension if you can. It's full of handy functions and classes to assist in i18n. For instance you can use the following snippet to generate a "best-fit" language for your visitor from those you have available:

$langs = array('en', 'fr', 'de');
$preferences = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$locale = Locale::lookup($langs, $preferences);

除了直接逐字替换之外,还有很多需要考虑的问题.不同的语言具有不同的格式约定.此 Zend文章中有很多信息,可能被证明是有用的.

There's also a lot more to think about than straight word-for-word replacements. Different languages have different formatting conventions. There's a lot of information in this Zend article that might prove useful.

我的代码使用XSL进行个人模板制作,因此在翻译时,我使用XML语言文件和XPath查询,以便模板可以直接理解该语言文件,但PHP也可以使用 MessageFormatter .

My code uses XSL for templating so personally, when it comes to translations I use XML language files and XPath queries so that the language file can be understood directly by templates but is also available to PHP for formatting strings with MessageFormatter.

老实说,我只是为了提高性能,避免使用数据库作为存储.

I'd avoid using the database as a store to be honest, simply for performance.

这篇关于多语言PHP应用程序:最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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