In this example, we translate messages in several languages. We use the language files of several multilingual applications. We use all the available adapters. We also use a custom adapter.
Components used in this example
Working with translations
class MyTranslate
{
Processing the translations- We get the adapter, the language file, the message, and the action to perform.
- We set the cache.
- We verify the locale or we search the locale.
- We get the adapter parameters.
- We instantiate the adapter. The translation files are added individually as needed or scanned automatically.
- We get the messages to translate in the default locale of the adapter.
- We execute the action.
- If we catch an exception, we return the error message.
public function process()
{
// We get the adapter, the language file, the message, and the action to perform.
list($source, $locale, $message, $topMessages, $useSimilar, $useDefault,
$action, $disableNotices) = $this->_getParameters();
$available = array();
$messages = array();
try {
// We set the cache.
$this->_setCache();
// We verify the locale or we search the locale.
$locale = Zend_Locale::findLocale($locale);
// We get the adapter parameters.
list($adapter, $dir, $default, $pattern, $file, $useMsgKey, $options) =
MyAdapter::parseSource($source, $disableNotices);
// We instantiate the adapter.
// The translation files are added individually as needed or scanned automatically.
if ($file) {
list($translate, $available) = MyAdapter::setFileTranslate(
$adapter, $dir, $locale, $default, $options, $useSimilar, $file);
} else {
list($translate, $available) = MyAdapter::setScanTranslate(
$adapter, $dir, $locale, $default, $options, $useSimilar);
}
// We get the messages to translate in the default locale of the adapter.
$messages = MyAdapter::getMessages(
$translate, $default, $useMsgKey, $topMessages) and
isset($messages[$message]) or $message = key($messages);
// We execute the action.
switch($action) {
case 'clearCache':
case 'getCache':
case 'hasCache':
$result = Zend_Translate::$action();
break;
case 'getAdapter':
case 'getList':
case 'getLocale':
case 'getMessageIds':
case 'getMessages':
case 'getOptions':
case 'toString':
$result = $translate->$action();
break;
case 'isAvailable':
$result = $translate->isAvailable($locale);
break;
case 'isTranslated':
if ($messages) {
$result = $translate->isTranslated($messages[$message][0]);
} else {
$result = 'Language file empty!';
}
break;
case 'view':
$pattern = str_replace('LOCALE', $locale, $pattern);
$pattern = str_replace('FILE', $file, $pattern);
@list($view) = glob("$dir/$pattern");
$result = $view? file_get_contents($view) : 'NO FILE!';
break;
case 'translate':
default:
if ($messages) {
$result = MyAdapter::translate($translate, $message,
$messages, $locale, $default, $useDefault);
} else {
$result = 'Language file empty!';
}
}
} catch (Exception $e) {
// If we catch an exception, we return the error message.
$result = $e->getMessage();
}
return array($source, $locale, $message, $topMessages, $useSimilar, $useDefault,
$action, $disableNotices, $available, $messages, $result);
}
Extraction of the parameters from the GET request
private function _getParameters()
{
$source = isset($_GET['source'])? $_GET['source'] : null;
$locale = empty($_GET['locale'])? null : $_GET['locale'];
$useSimilar = empty($_GET)? 1 : !empty($_GET['use-similar']);
$useDefault = empty($_GET)? 0 : !empty($_GET['use-default']);
$message = isset($_GET['message'])? $_GET['message'] : null;
$topMessages = empty($_GET)? 1 : !empty($_GET['top-messages']);
$action = isset($_GET['action'])? $_GET['action'] : null;
$disableNotices = empty($_GET)? 1 : !empty($_GET['disable-notices']);
return array($source, $locale, $message, $topMessages, $useSimilar, $useDefault,
$action, $disableNotices);
}
Setting the cache
private function _setCache()
{
$frontendOptions = array('lifetime' => 3600 * 24, 'automatic_serialization' => true);
$backendOptions = array('cache_dir' => sys_get_temp_dir());
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
Zend_Locale::setCache($cache);
Zend_Translate::setCache($cache);
}
}
No comments:
Post a Comment