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

Getting Started with Zend Framework (2)—Multiple Language Support

Dec 17, 2016 am 10:26 AM

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:

<?php
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 "<h1>Hello World!</h1>" with:

&lt ;h1><?php echo $this->translate('hello_world'); ?></h1>


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 of this Website
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 Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix May 05, 2023 pm 04:01 PM

Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix

How to identify Windows upgrade issues using SetupDiag on Windows 11/10 How to identify Windows upgrade issues using SetupDiag on Windows 11/10 Apr 17, 2023 am 10:07 AM

How to identify Windows upgrade issues using SetupDiag on Windows 11/10

SCNotification has stopped working [5 steps to fix it] SCNotification has stopped working [5 steps to fix it] May 17, 2023 pm 09:35 PM

SCNotification has stopped working [5 steps to fix it]

Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Apr 17, 2023 pm 02:25 PM

Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022

KB5012643 for Windows 11 breaks .NET Framework 3.5 apps KB5012643 for Windows 11 breaks .NET Framework 3.5 apps May 09, 2023 pm 01:07 PM

KB5012643 for Windows 11 breaks .NET Framework 3.5 apps

How to use ACL (Access Control List) for permission control in Zend Framework How to use ACL (Access Control List) for permission control in Zend Framework Jul 29, 2023 am 09:24 AM

How to use ACL (Access Control List) for permission control in Zend Framework

PHP Implementation Framework: Zend Framework Getting Started Tutorial PHP Implementation Framework: Zend Framework Getting Started Tutorial Jun 19, 2023 am 08:09 AM

PHP Implementation Framework: Zend Framework Getting Started Tutorial

Cooler Master and Framework launch innovative mini case kit, compatible with laptop motherboards Cooler Master and Framework launch innovative mini case kit, compatible with laptop motherboards Dec 15, 2023 pm 05:35 PM

Cooler Master and Framework launch innovative mini case kit, compatible with laptop motherboards

See all articles