search
Homephp教程PHP开发Getting Started with Zend Framework (2)—Multiple Language Support

If your project wants to support multi-language versions, you need to use Zend_Translate. The detailed documentation of Zend_Translate is here, but if you want to be lazy, it is also very simple. The View Helpers document introduces how to use Translate Helper to easily implement multi-language support.

1. Prepare translation files

Zend_Translate supports translation files in multiple formats. Which format to choose can be found here. If there are not many entries (less than 5000), then you can consider using the most intuitive array format, and you can write it to a php file. Assume that we need a Chinese version support. The translation file is named zh_cn.php and placed in the languages ​​folder parallel to application. The content of the file is as follows:

return array(
'hello_world' => 'Hello!',
);


2. Load the translation file

Edit the html/index.php file, in Before the front controller runs, insert the following code:

require_once 'Zend/Registry.php';
require_once 'Zend/Translate.php';
$adapter = new Zend_Translate('array', $rootPath . '/languages/ zh_cn.php', 'zh');
Zend_Registry::set('Zend_Translate', $adapter);

The function of the above code is to load zh_cn.php and save it as a global variable. Zend_Registry can be regarded as a global variable container.

Note: When saving to Zend_Registry, the key value must be Zend_Translate, otherwise, the expected results will not be obtained.


3. Use the translation entry in the view

Edit the application/views/scripts/index/index.phtml file and replace the original "

Hello World!

" with:

< ;h1>translate('hello_world'); ?>


4. View the page

At this time, what you see in the browser should be "You good!".

The above is the introduction to Zend Framework (2) - multi-language support. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool