Additional features for translationThere are several additional features which are supported by Zend_Translate. Read here for these additional informations. Options for adaptersOptions 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 by giving them to addTranslation(). And you can use the method setOptions() to set options permanent. Example #1 Using translation options
Here you can find all available options for the different adapters with a description of their usage:
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. Handling languagesWhen 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. Example #2 Handling languages with adapters
Automatical handling of languagesNote 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: Example #3 Automatically language detection
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.
Using a country as languageYou can also use a country as locale parameter. This could be useful when you provide your user with flags, which represent the country in which he lives, and when he selects his flag, he would automatically get the default language for this country. For example, when the user selected US then you would get en_US in return as locale which is being used. This leads automatically to the language en which is the default language for the country US.
Automatic source detectionZend_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.
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. Example #4 Scanning a directory structure for sources
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.
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: Language through naming directoriesOne 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: Example #5 Directory scanning for languages
Language through filenamesAnother 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: Example #6 Filename scanning for languages
Complete filenameHaving the whole file named after the language is the simplest way but only viable if you have only one file per language.
Extension of the fileAnother 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.
Filename tokensZend_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 '-'.
The first found string delimited by a token which can be interpreted as a locale will be used. See the following example for details.
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.
Ignoring special files and directoriesSometimes it is useful to exclude files or even directories from being added automatically. Therefor you can use the ignore option which accepts 3 possible usages. Ignore a special directory or filePer default Zend_Translate is set to ignore all files and directories beginning with '/.'. This means that all SVN files will be ignored. You can set your own syntax by giving a string for the ignore option. The directory separator will be attached automatically and has to be omitted. The above example will ignore all files and directories beginning with test. This means for example /test/en.mo, /testing/en.mo and /dir/test_en.mo. But it would still add /mytest/en.mo or /dir/atest.mo.
Ignore several directories or filesYou can also ignore several files and directories. Instead of a string, you can simply give an array with all wished names which will be ignored. In the above case all 3 syntax will be ignored. But still they have to begin with the syntax to be detected and ignored. Ignore specific namesTo ignore files and directories which are not beginning with a defined syntax but have a special syntax anywhere within their name you can use a regular expression. To use a regular expression the array key of the ignore option has to begin with regex. In the above case we defined 2 regular expressions. The files and directories will always being searched with all given regular expressions. In our example this means that any files which contains test anywhere in their name will be ignored. Additionally all files and directories which end with deleted will not be added as translation. Routing for translationsNot every message ID can be translated. But sometimes is can be useful to output the translation from another language instead of returning the message ID itself. You can archive this by using the route option. You can add one route for every language. See the following example: The above returns a english translation for all messages which can not be translated to french. And it returns a french translation for all messages which can not be translated to german. It will even return an english translation for all messages which can wether be translated to german nor to french. So you can even define a complete translation chain. This feature seems ot be interesting for anyone. But be aware that returning translations for wrong or other languages can be problematic when the user does not understand this language. So you should always use this feature sparingly. Combining multiple translation sourcesWhen you are working with multiple translations you may come into a situation where you want to use different source types. For example the resource files which are provided by the framework and your own translations which are available by using the gettext adapter. By combining multiple translation adapters you can use them within one instance. See the following example:
Now the first instance holds all translations from the second instance and you can use it within the application even if you used different source types.
When you are scanning for directories you may additionally want to use only one defined language. The predefined resources for example are available in more than 10 languages. But your application is not available in all of those language. Therefor you can also add only one language from the second adapter.
This allows you still to scan through the directories and still add only those languages which are relevant for your application. Checking for translationsNormally 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. Example #7 Checking if a text is translatable
How to log not found translationsWhen 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: Example #8 Log translations
Now you will have a new notice in the log: Untranslated message within 'de': unknown string.
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: Example #9 Self defined log messages
Additionally you are also able to change the priority which is used to write the message into the log. Per default the priority Zend_Log::NOTICE is used. It equals with 5. When you want to change the priority you can use any of Zend_Log's priorities. See the following example: Example #10 Self defined log priority
Accessing source dataSometimes 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. When you want to know the message ID for a given translation then you can use the getMessageId() method. 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.
Example #11 Handling languages with adapters
|