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 5,000), 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 the application. The content of the file is as follows:
<?php
return array(
'hello_world' => 'Hello! ',
);
2. Load the translation file
and edit the html/index.php file. Before running the front-end controller, 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. Edit the application/views/scripts/index/index.phtml file using the translation entry in the view
, and replace the original "<h1>Hello World!</h1>" with:
<h1><?php echo $this- >translate('hello_world'); ?></h1>
4. View the page.
At this time, what you see in the browser should be "Hello!".