The next step is to use the adapter within your code.
例 56.1. Example of single-language PHP code
print "Example\n"; print "=======\n"; print "Here is line one\n"; print "Today is the " . date("d.m.Y") . "\n"; print "\n"; print "Here is line two\n";
The example above shows some output with no support for translation. You probably write your code in your native language. Generally you need to translate not only the output, but also error and log messages.
The next step is to integrate Zend Translate into your existing code. Of course it is much easier if you had already written your code with translation in mind, than changing your code afterwards.
例 56.2. Example of multi-lingual PHP code
$translate = new Zend_Translate('gettext', '/my/path/source-de.mo', 'de'); $translate->addTranslation('/path/to/translation/fr-source.mo', 'fr'); print $translate->_("Example") . "\n"; print "=======\n"; print $translate->_("Here is line one") . "\n"; printf($translate->_("Today is the %1\$s") . "\n", date('d.m.Y')); print "\n"; $translate->setLocale('fr'); print $translate->_("Here is line two") . "\n";
Now let's take a deeper look into what has been done and how to
integrate Zend_Translate
into your own code.
Create a new Zend_Translate
object and define the base adapter:
$translate = new Zend_Translate 'gettext', '/path/to/translation/source-de.mo', 'de' );
In this example we chose the Gettext Adapter. We place our file source-de.mo into the directory /path/to/translation. The gettext file will have German translation included, and we also added another language source for French.
The next step is to wrap all strings which are to be translated. The simplest approach is to have only simple strings or sentences like this:
print $translate->_("Example") . "\n"; print "=======\n"; print $translate->_("Here is line one") . "\n";
Some strings do not needed to be translated. The separating line is always a separating line, even in other languages.
Having data values integrated into a translation string is also supported through the use of embedded parameters.
printf($translate->_("Today is the %1\$s") . "\n", date("d.m.Y"));
Instead of print()
, use the printf()
function and replace all parameters with %1\$s
parts.
The first is %1\$s
, the second is %2\$s
,
and so on. This way a translation can be done without knowing
the exact value. In our example, the date is always the actual day,
but the string can be translated without the knowledge of the actual
day.
Each string is identified in the translation storage by a message ID. You can use message IDs instead of strings in your code, like this:
print $translate->_(1) . "\n"; print "=======\n"; print $translate->_(2) . "\n";
But doing this has several disadvantages:
You can not see what your code should output just by viewing your code.
Also you will have problems if some strings are not translated.
You must always keep in mind how translation works.
First Zend_Translate
checks whether the specified language has a translation
for the given message ID or string.
If no translation string has been found it refers to the next lower
level language as defined within Zend_Locale
.
So "de_AT" becomes
"de" only.
If there is no translation found for
"de" either,
then the original message is returned.
This way you always have an output, even in case the message translation
does not exist in your message storage.
Zend_Translate
never throws an error or exception when translating
strings.
Your next step is to create the translation sources for the languages you want to translate. Every adapter is created its own way as described here, but there are common features applicable for all adapters.
You have to decide where to store your translation source files.
Using Zend_Translate
you are not restricted in any way.
The following structures are preferable:
Single structured source
/application/ /languages/ /languages/lang.en /languages/lang.de /library/
Positive: all source files for every languages are stored in one directory. No splitting of related files.
Language structured source
/application/ /languages/ /languages/en/ /languages/en/first.en /languages/en/second.en /languages/de/ /languages/de/first.de /languages/de/second.de /library
Positive: Every language is stored in their own directories. Easy translation, as every language team has to translate only one directory. Also the usage of multiple files is transparent.
Application structured source
/application/ /application/languages/ /application/languages/first.en /application/languages/first.de /application/languages/second.en /application/languages/second.de /library/
Positive: all source files for every language are stored in one directory. No splitting of related files.
Negative: having multiple files for the same language can be problematic.
Gettext structured source
/application/ /languages/ /languages/de/ /languages/de/LC_MESSAGES/ /languages/de/LC_MESSAGES/first.mo /languages/de/LC_MESSAGES/second.mo /languages/en/ /languages/en/LC_MESSAGES/ /languages/en/LC_MESSAGES/first.mo /languages/en/LC_MESSAGES/second.mo /library/
Positive: existing gettext sources can be used without changing structure.
Negative: having sub-sub directories may be confusing for people who have not used gettext before.
File structured source
/application/ /application/models/ /application/models/MyModel.html /application/models/MyModel.de /application/models/MyModel.en /application/controllers/ /application/controllers/MyController.html /application/controllers/MyController.de /application/controllers/MyController.en /library/
Positive: translation files are localted near their source.
Negative: too many and also small translation files result in being tendious to translate. Also every file has to be added as translation source.
Single structured and language structured source files are most
usable for Zend_Translate
.
So now, that we know which structure we want to have, we should create our translation source files.
Array source files are plain arrays. But you have to define them manually since there is no tool to aid this. But because they are so simple, it's the fastest way to look up messages if your code works as expected. It's generally the best adapter to get started with translation business.
$english = array( 'message1' => 'message1', 'message2' => 'message2', 'message3' => 'message3'); $german = array( 'message1' => 'Nachricht1', 'message2' => 'Nachricht2', 'message3' => 'Nachricht3'); $translate = new Zend_Translate('array', $english, 'en'); $translate->addTranslation($deutsch, 'de');
Since release 1.5 it is also supported to have arrays included within an external file.
You just have to provide the filename and Zend_Translate
will automatically
include it and look for the array. See the following example for details:
// myarray.html return array( 'message1' => 'Nachricht1', 'message2' => 'Nachricht2', 'message3' => 'Nachricht3'); // controller $translate = new Zend_Translate('array', '/path/to/myarray.html', 'de');
![]() |
注意 |
---|---|
Files which do not return an array will fail to be included. Also any output within this file will be ignored and suppressed. |
Gettext source files are created by GNU's gettext library. There are several free tools available that can parse your code files and create the needed gettext source files. These have the extension *.mo and they are binary files. An open source tool for creating the files is poEdit. This tool also supports you during the translation process itself.
// We accume that we have created the mo files and translated them $translate = new Zend_Translate('gettext', '/path/to/english.mo', 'en'); $translate->addTranslation('/path/to/german.mo', 'de');
As you can see the adapters are used exactly the same way, with one small difference: change array to gettext. All other usages are exactly the same as with all other adapters. With the gettext adapter you no longer have to be aware of gettext's standard directory structure, bindtextdomain and textdomain. Just give the path and filename to the adapter.
![]() |
注意 |
---|---|
You should always use UTF-8 as source encoding. Otherwise you will have problems when using two different source encodings. E.g. one of your source files is encoded with ISO-8815-11 and another one with CP815. You can set only one encoding for your source file, so one of your languages probably will not display correctly. UTF-8 is a portable format which supports all languages. When using UTF-8 for all languages, you will eliminate the problem of incompatible encodings. |
Many gettext editors add adapter informations as empty translation string.
This is the reason why empty strings are not translated when using the
gettext adapter. Instead they are erased from the translation table and
provided by the getAdapterInfo()
method. It will return
the adapter informations for all added gettext files as array using the
filename as key.
// Getting the adapter informations $translate = new Zend_Translate('gettext', '/path/to/english.mo', 'en'); print_r($translate->getAdapterInfo());
TMX source files are a new industry standard. They have the advantage of being XML files and so they are readable by every editor and of course by humans. You can either create TMX files manually with a text editor, or you can use a special tool. But most tools currently available for creating TMX source files are not freely available.
例 56.3. Example TMX file
<?xml version="1.0" ?> <!DOCTYPE tmx SYSTEM "tmx14.dtd"> <tmx version="1.4"> <header creationtoolversion="1.0.0" datatype="winres" segtype="sentence" adminlang="en-us" srclang="de-at" o-tmf="abc" creationtool="XYZTool" > </header> <body> <tu tuid='message1'> <tuv xml:lang="de"><seg>Nachricht1</seg></tuv> <tuv xml:lang="en"><seg>message1</seg></tuv> </tu> <tu tuid='message2'> <tuv xml:lang="en"><seg>message2</seg></tuv> <tuv xml:lang="de"><seg>Nachricht2</seg></tuv> </tu>
$translate = new Zend_Translate('tmx', 'path/to/mytranslation.tmx', 'en');
TMX files can have several languages within the same file.
All other included languages are added automatically,
so you do not have to call addLanguage()
.
If you want to have only specified languages from the source translated
you can set the option 'defined_language
' to true
.
With this option you can add the wished languages explicitly with
addLanguage()
. The default value for this option is to add all
languages.
CSV source files are small and human readable. If your customers want to translate their own, you will probably use the CSV adapter.
例 56.4. Example CSV file
#Example csv file message1;Nachricht1 message2;Nachricht2
$translate = new Zend_Translate('csv', '/path/to/mytranslation.csv', 'de'); $translate->addTranslation('path/to/other.csv', 'fr');
There are three different options for the CSV adapter.
You can set 'delimiter
', 'limit
' and
'enclosure
'.
The default delimiter for CSV string is ';
', but
with the option 'delimiter
'
you can decide to use another one.
The default limit for a line within a CSV file is '0
'. This means
that the end of a CSV line is searched automatically. If you set
'limit
' to any value, then the CSV file will be
read faster, but any line exceeding this limit will be truncated.
The default enclosure to use for CSV files is '"
'. You can
set a different one using the option 'enclosure
'.
例 56.5. Second CSV file example
# Example CSV file "message,1",Nachricht1 message2,"Nachricht,2" "message3,",Nachricht3
$translate = new Zend_Translate( 'csv', '/path/to/mytranslation.csv', 'de', array('delimiter' => ',')); $translate->addTranslation('/path/to/other.csv', 'fr');
INI source files are human readable but normally not very small as they also include other data beside translations. If you have data which shall be editable by your customers you can use the INI adapter.
例 56.6. Example INI file
[Test] ;TestPage Comment Message_1="Nachricht 1 (de)" Message_2="Nachricht 2 (de)" Message_3="Nachricht :3 (de)"
$translate = new Zend_Translate('ini', '/path/to/mytranslation.ini', 'de'); $translate->addTranslation('/path/to/other.ini', 'it');
INI files have several restrictions. If a value in the ini file contains any
non-alphanumeric characters it needs to be enclosed in double-quotes ("
).
There are also reserved words which must not be used as keys for ini files.
These include: null
, yes
, no
, true
,
and false
. Values null
, no
and false
results
in ""
, yes
and true
results in 1
. Characters {}|&~![()"
must not be used anywhere
in the key and have a special meaning in the value. Do not use them as it will
produce unexpected behaviour.
Options can be used with all adapters. Of course the options are different for all adapters.
You can set options when you create the adapter. Actually there is one option which is available
to all adapters: 'clear
' sets if translation data should be added to existing
one or not. Standard behaviour is to add new translation data to existing one. But the
translation data is only cleared for the selected language. So other languages remain
untouched.
You can set options temporarily when using addTranslation($data, $locale, array $options = array())
as third and optional parameter. And you can use the method setOptions()
to
set the options permanently.
例 56.7. Using translation options
// define ':' as separator for the translation source files $options = array('delimiter' => ':'); $translate = new Zend_Translate( 'csv', '/path/to/mytranslation.csv', 'de', $options); ... // clear the defined language and use new translation data $options = array('clear' => true); $translate->addTranslation('/path/to/new.csv', 'fr', $options);
Here you can find all available options for the different adapters with a description of their usage:
表 56.2. Options for translation adapters
Option | Adapter | Description | Default value |
---|---|---|---|
clear | all | If set to true, the already read translations will be cleared. This can be used instead of creating a new instance when reading new translation data | false |
disableNotices | all | If set to true, all notices regarding not available translations will be disabled. You should set this option to true in production environment | false |
ignore | all |
All directories and files beginning with this prefix will be ignored when
searching for files. This value defaults to '.'
which leads to the behavior that all hidden files will be ignored. Setting this
value to 'tmp' would mean that directories and files like
'tmpImages' and 'tmpFiles'
would be ignored as well as all subsequent directories
|
. |
log | all |
An instance of Zend_Log where untranslated messages and notices will be
written to
|
null |
logMessage | all | The message which will be written into the log | Untranslated message within '%locale%': %message% |
logUntranslated | all | When this option is set to true, all message IDs which can not be translated will be written into the attached log | false |
scan | all |
If set to null, no scanning of the directory structure will be done.
If set to Zend_Translate::LOCALE_DIRECTORY the locale will be detected within the
directory. If set to Zend_Translate::LOCALE_FILENAME the locale will be detected
within the filename. See 第 56.3.9 节 “Automatic source detection”
for details
|
null |
delimiter | Csv | Defines which sign is used as delimiter for separating source and translation | ; |
enclosure | Csv | Defines the enclosure character to be used. Defaults to a doublequote | " |
length | Csv | Defines the maximum length of a csv line. When set to 0 it will be detected automatically | 0 |
When you want to have self defined options, you are also able to use them within all adapters.
The setOptions()
method can be used to define your option. setOptions()
needs an array with the options you want to set. If an given option exists it will be signed over.
You can define as much options as needed as they will not be checked by the adapter. Just make sure
not to overwrite any existing option which is used by an adapter.
To return the option you can use the getOptions()
method. When getOptions()
is called without a parameter it will return all options set. When the optional parameter is given
you will only get the specified option.
When working with different languages there are a few methods which will be useful.
The getLocale()
method can be used to get the currently set language. It can either hold
an instance of Zend_Locale
or the identifier of a locale.
The setLocale()
method sets a new standard language for translation. This prevents the
need of setting the optional language parameter more than once to the translate()
method.
If the given language does not exist, or no translation data is available for the language,
setLocale()
tries to downgrade to the language without the region if any was given.
A language of en_US
would be downgraded to en
. When even the downgraded
language can not be found an exception will be thrown.
The isAvailable()
method checks if a given language is already available. It returns
true
if data for the given language exist.
And finally the getList()
method can be used to get all currently set languages for an adapter
returned as array.
例 56.8. Handling languages with adapters
// returns the currently set language $actual = $translate->getLocale(); // you can use the optional parameter while translating echo $translate->_("my_text", "fr"); // or set a new language $translate->setLocale("fr"); echo $translate->_("my_text"); // refer to the base language // fr_CH will be downgraded to fr $translate->setLocale("fr_CH"); echo $translate->_("my_text"); // check if this language exist if ($translate->isAvailable("fr")) { // language exists }
Note that as long as you only add new translation sources with the addTranslation()
method Zend_Translate
will automatically set the best fitting language for your
environment when you use one of the automatic locales which are 'auto
' or 'browser
'. So
normally you will not need to call setLocale()
. This should only be used in
conjunction with automatic source detection.
The algorithm will search for the best fitting locale depending on the user's browser and your environment. See the following example for details:
例 56.9. Automatically language detection
// Let's expect the browser returns these language settings: // HTTP_ACCEPT_LANGUAGE = "de_AT=1;fr=1;en_US=0.8"; // Example 1: // When no fitting language is found, the message ID is returned $translate = new Zend_Translate( 'gettext', 'my_it.mo', 'auto', array('scan' => Zend_Translate::LOCALE_FILENAME)); // Example 2: // Best found fitting language is 'fr' $translate = new Zend_Translate( 'gettext', 'my_fr.mo', 'auto', array('scan' => Zend_Translate::LOCALE_FILENAME)); // Example 3: // Best found fitting language is 'de' ('de_AT' will be degraded) $translate = new Zend_Translate( 'gettext', 'my_de.mo', 'auto', array('scan' => Zend_Translate::LOCALE_FILENAME)); // Example 4: // Returns 'it' as translation source and overrides the automatic settings $translate = new Zend_Translate( 'gettext', 'my_it.mo', 'auto', array('scan' => Zend_Translate::LOCALE_FILENAME)); $translate->addTranslation('my_ru.mo', 'ru'); $translate->setLocale('it_IT');
After setting a language manually with the setLocale()
method the automatic
detection will be switched off and overridden.
If you want to use it again, you can set the language
auto with setLocale()
which will reactivate
the automatic detection for Zend_Translate
.
Since Zend Framework 1.7.0 Zend_Translate
also recognises an application
wide locale. You can simply set a Zend_Locale
instance to the registry like shown
below. With this notation you can forget about setting the locale manually with each instance
when you want to use the same locale multiple times.
// in your bootstrap file $locale = new Zend_Locale(); Zend_Registry::set('Zend_Locale', $locale); // default language when requested language is not available $defaultlanguage = 'en'; // somewhere in your application $translate = new Zend_Translate('gettext', 'my_de.mo'); if (!$translate->isAvailable($locale->getLanguage())) { // not available languages are rerouted to another language $translate->setLocale($defaultlanguage); } $translate->getLocale();
Zend_Translate
can detect translation sources automatically. So you don't have
to declare each source file manually. You can let Zend_Translate
do this job and
scan the complete directory structure for source files.
![]() |
注意 |
---|---|
Automatic source detection is available since Zend Framework version 1.5 . |
The usage is quite the same as initiating a single translation source with one difference. You must give a directory which has to be scanned instead a file.
例 56.10. Scanning a directory structure for sources
// assuming we have the following structure // /language/ // /language/login/login.tmx // /language/logout/logout.tmx // /language/error/loginerror.tmx // /language/error/logouterror.tmx $translate = new Zend_Translate('tmx', '/language');
So Zend_Translate
does not only search the given directory, but also all subdirectories for
translation source files. This makes the usage quite simple. But Zend_Translate
will ignore all
files which are not sources or which produce failures while reading the translation data. So you
have to make sure that all of your translation sources are correct and readable because you will
not get any failure if a file is bogus or can not be read.
![]() |
注意 |
---|---|
Depending on how deep your directory structure is and how much files are within this structure
it can take a long time for |
In our example we have used the TMX format which includes the language to be used within the source. But many of the other source formats are not able to include the language within the file. Even this sources can be used with automatic scanning if you do some pre-requisits as described below:
One way to include automatic language detection is to name the directories related to the language which is used for the sources within this directory. This is the easiest way and is used for example within standard gettext implementations.
Zend_Translate
needs the 'scan
' option to know that it should search the names of all
directories for languages. See the following example for details:
例 56.11. Directory scanning for languages
// assuming we have the following structure // /language/ // /language/de/login/login.mo // /language/de/error/loginerror.mo // /language/en/login/login.mo // /language/en/error/loginerror.mo $translate = new Zend_Translate( 'gettext', '/language', null, array('scan' => Zend_Translate::LOCALE_DIRECTORY));
![]() |
注意 |
---|---|
This works only for adapters which do not include the language within the source file. Using this option for example with TMX will be ignored. Also language definitions within the filename will be ignored when using this option. |
![]() |
注意 |
---|---|
You should be aware if you have several subdirectories under the same
structure. Assuming we have a structure like
|
Another way to detect the language automatically is to use special filenames. You can either
name the complete file or parts of a file after the used language. To use this way of detection
you will have to set the 'scan
' option at initiation. There are several ways of naming the
sourcefiles which are described below:
例 56.12. Filename scanning for languages
// assuming we have the following structure // /language/ // /language/login/login_en.mo // /language/login/login_de.mo // /language/error/loginerror_en.mo // /language/error/loginerror_de.mo $translate = new Zend_Translate( 'gettext', '/language', null, array('scan' => Zend_Translate::LOCALE_FILENAME));
Having the whole file named after the language is the simplest way but only viable if you have only one file per language.
/languages/ /languages/en.mo /languages/de.mo /languages/es.mo
Another simple way to use the extension of the file for language detection. But this may be confusing since you will no longer have an idea which extension the file originally had.
/languages/ /languages/view.en /languages/view.de /languages/view.es
Zend_Translate
is also capable of detecting the language if it is included within the
filename. But if you go this way you will have to separate the language with a token.
There are three supported tokens which can be used: a dot '.', an underscore '_', or
a hyphen '-'.
/languages/ /languages/view_en.mo -> detects english /languages/view_de.mo -> detects german /languages/view_it.mo -> detects italian
The first found string delimited by a token which can be interpreted as a locale will be used. See the following example for details.
/languages/ /languages/view_en_de.mo -> detects english /languages/view_en_es.mo -> detects english and overwrites the first file /languages/view_it_it.mo -> detects italian
All three tokens are used to detect the locale. When the filename contains multiple tokens, the first found token depends on the order of the tokens which are used. See the following example for details.
/languages/ /languages/view_en-it.mo -> detects english because '_' will be used before '-' /languages/view-en_it.mo -> detects italian because '_' will be used before '-' /languages/view_en.it.mo -> detects italian because '.' will be used before '_'
Normally text will be translated without any computation. But sometimes it is necessary to
know if a text is translated or not, therefor the isTranslated()
method can be used.
isTranslated($messageId, $original = false, $locale = null)
takes
the text you want to check as its first parameter, and as optional third parameter the locale
for which you want to do the check. The optional second parameter declares whether translation
is fixed to the declared language or a lower set of translations can be used. If you have a text which
can be returned for 'en' but not for 'en_US' you will normally get the translation returned, but by
setting $original
to true, isTranslated()
will return false.
例 56.13. Checking if a text is translatable
$english = array( 'message1' => 'Nachricht 1', 'message2' => 'Nachricht 2', 'message3' => 'Nachricht 3'); $translate = new Zend_Translate('array', $english, 'de_AT'); if ($translate->isTranslated('message1')) { print "'message1' can be translated"; } if (!($translate->isTranslated('message1', true, 'de'))) { print "'message1' can not be translated to 'de'" . " as it's available only in 'de_AT'"; } if ($translate->isTranslated('message1', false, 'de')) { print "'message1' can be translated in 'de_AT' as it falls back to 'de'"; }
When you have a bigger site or you are creating the translation files manually, you often have
the problem that some messages are not translated. But there is an easy solution for you when you
are using Zend_Translate
.
You have to follow two or three simple steps. First, you have to create an instance of
Zend_Log
. Then you have to attach this instance to Zend_Translate
.
See the following example:
例 56.14. Log translations
$translate = new Zend_Translate('gettext', $path, 'de'); // Create a log instance $writer = new Zend_Log_Writer_Stream('/path/to/file.log'); $log = new Zend_Log($writer); // Attach it to the translation instance $translate->setOptions(array( 'log' => $log, 'logUntranslated' => true)); $translate->translate('unknown string');
Now you will have a new notice in the log: Untranslated message within 'de': unknown string
.
![]() |
注意 |
---|---|
You should note that any translation which can not be found will be logged. This means all translations when a user requests a language which is not supported. Also every request for a message which can not be translated will be logged. Be aware, that 100 people requesting the same translation, will result 100 logged notices. |
This feature can not only be used to log messages but also to attach this untranslated messages into an empty translation file. To do so you will have to write your own log writer which writes the format you want to have and strips the prepending "Untranslated message".
You can also set the 'logMessage
' option when you want to have your own log message.
Use the '%message%
' token for placing the messageId within your log message, and the
'%locale%
' token for the requested locale. See the following example for a self
defined log message:
例 56.15. Self defined log messages
$translate = new Zend_Translate('gettext', $path, 'de'); // Create a log instance $writer = new Zend_Log_Writer_Stream('/path/to/file.log'); $log = new Zend_Log($writer); // Attach it to the translation instance $translate->setOptions(array( 'log' => $log, 'logMessage' => "Missing '%message%' within locale '%locale%'", 'logUntranslated' => true)); $translate->translate('unknown string');
Sometimes it is useful to have access to the translation source data. Therefor the following two functions are provided.
The getMessageIds($locale = null)
method returns all known message IDs as array.
The getMessages($locale = null)
method returns the complete translation source as
an array. The message ID is used as key and the translation data as value.
Both methods accept an optional parameter $locale
which, if set, returns the
translation data for the specified language. If this parameter is not given, the actual set
language will be used. Keep in mind that normally all translations should be available in all
languages. Which means that in a normal situation you will not have to set this parameter.
Additionally the getMessages()
method can be used to return the complete
translation dictionary using the pseudo-locale 'all'. This will return all available
translation data for each added locale.
![]() |
注意 |
---|---|
Attention: the returned array can be very big, depending on the number of added locales and the amount of translation data. |
例 56.16. Handling languages with adapters
// returns all known message IDs $messageIds = $translate->getMessageIds(); print_r($messageIds); // or just for the specified language $messageIds = $translate->getMessageIds('en_US'); print_r($messageIds); // returns all the complete translation data $source = $translate->getMessages(); print_r($source);