Getting started with ZendFramework (2)-multi-language support

Source: Internet
Author: User
ZendFramework entry (2)-multi-language support, read ZendFramework entry (2)-multi-language support. If your project wants to support multi-language versions, you need to use Zend_Translate. The detailed document of Zend_Translate is here, but it is also very easy to be lazy. In the ViewHelpers document, I will introduce how to ">

Zend_Translate is required if your project supports multilingual versions. The detailed documentation of Zend_Translate is here. However, if you want to be lazy, it is also very simple. The View Helpers document describes how to use Translate Helper to easily implement multi-language support.

1. Prepare the translation file

Zend_Translate supports multiple formats of translation files. For more information about the format, see here. If there are not many entries (less than 5000 entries), you can consider using the most intuitive array format and write it into a php file. Assume that a Chinese version is required. The translation file is named zh_cn.php and placed in the ages folder parallel to the application. The file content is as follows:

Return array (
'Hello _ World' => 'hello! ',
);


2. Load the translation file

Edit the html/index. php file and insert the following code before the front-end controller runs:

Require_once 'zend/Registry. Php ';
Require_once 'zend/Translate. Php ';
$ Adapter = new Zend_Translate ('array', $ rootPath. '/ages/zh_cn.php', 'zh ');
Zend_Registry: set ('zend _ Translate', $ adapter );

The above code loads zh_cn.php and saves it as a global variable. Zend_Registry can be viewed as a global variable container.

Note: When saving to Zend_Registry, the key value must be Zend_Translate. Otherwise, the expected result cannot be obtained.


3. Use translation entries in the view

Edit the application/views/scripts/index. phtml file and convert the original "Hello World !" Replace:

Translate ('Hello _ world');?>


4. View the page

In this case, the browser should see "Hello !".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.