PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Zend_Application_Resource_Locale

Zend_Application_Resource_Locale can be used to set an application-wide locale which is then used in all classes and components which work with localization or internationalization. By default the locale is saved in a Zend_Registry entry with key 'Zend_Locale'.

There are basically three usecases for the Locale Resource Plugin. Each of them should be used depending on the applications need.

Autodetect the locale to use

Without specifying any options for Zend_Application_Resource_Locale, Zend_Locale will detect the locale, which your application will use, automatically.

This detection works because your client sends the wished language within his HTTP request. Normally the clients browser sends the languages he wants to see, and Zend_Locale uses this information for detection.

But there are 2 problems with this approach:

  • The browser could be setup to send no language

  • The user could have manually set a locale which does not exist

In both cases Zend_Locale will fallback to other mechanism to detect the locale:

  • When a locale has been set which does not exist, Zend_Locale tries to downgrade this string.

    When, for example, en_ZZ is set it will automatically be degraded to en. In this case en will be used as locale for your application.

  • When the locale could also not be detected by downgrading, the locale of your environment (web server) will be used. Most available environments from Web Hosters use en as locale.

  • When the systems locale could not be detected Zend_Locale will use it's default locale, which is set to en per default.

For more informations about locale detection take a look into this chapter on Zend_Locale's automatic detection.

Autodetect the locale and adding a own fallback

The above autodetection could lead to problems when the locale could not be detected and you want to have another default locale than en. To prevent this, Zend_Application_Resource_Locale allows you to set a own locale which will be used in the case that the locale could not be detected.

Example 38. Autodetect locale and setting a fallback

The following snippet shows how to set a own default locale which will be used when the client does not send a locale himself.

; Try to determine automatically first,
; if unsuccessful, use nl_NL as fallback.
resources.locale.default = "nl_NL"

Forcing a specific locale to use

Sometimes it is useful to define a single locale which has to be used. This can be done by using the force option.

In this case this single locale will be used and the automatic detection is turned off.

Example 39. Defining a single locale to use

The following snippet shows how to set a single locale for your entire application.

; No matter what, the nl_NL locale will be used.
resources.locale.default = "nl_NL"
resources.locale.force = true

Configure cache template

When you have set no cache, Zend_Locale will set itself a cache with the file backend by default. But if you want to choose the backend or others options, you can use the name of a cache template or an instance of Zend_Cache_Core. For more informations look into the section called “Speed up Zend_Locale and its subclasses”.

Example 40. Defining a cache template to use

; Optionally you can also the cache template to use for caching:
resources.locale.cache = "locale"

Zend Framework