Basic localization features


Enabling internationalization

A localized OFW can be enabled by following these steps:

  1. Add all the required locales to site/index.php (see the default and available locales below)
  2. Add the language selection logic to your default controller’s __load() method (see setting the current locale by domain below)
  3. If you are using the OFW WordPress plugin, you should enable WPML and add the necessary languages
  4. Create a set of language switcher links that point to the subdomains you set up

The default and available locales

There is a section in your site/index.php settings file where you can set the default and all of the available locales:

$zajconf['locale_default'] = 'en_US';
$zajconf['locale_available'] = 'hu_HU,en_US';

If you try to set the locale to one that is not defined here it will revert to the default locale.

Important! When you change the default locale, make sure that all language files you refer to are available in this default locale. Trying to load a language file that does not exist in the default locale could lead to fatal errors!

Setting an admin locale

Many times the admin panel of your site is not available in all of the localized languages of your public site. You can use the locale_admin setting to specify which locale should be used for the admin panel. As with other settings, this is set in site/index.php.

$zajconf['locale_admin'] = 'en_US';

Setting and getting current locale

At startup, the default locale is automatically set. You can make sure this is indeed the case by invoking $this->zajlib->lang->get() to get the current locale.

You can also change the locale by calling the set method: $this->zajlib->lang->set('sk_SK').

Set the locale automatically

On websites, it is recommended that you have a separate subdomain or domain for each language.

The easiest way is to use the automatic locale detection feature:

This will:

  1. look for a query string value like ?locale=en_US
  2. look for a query string value like ?lang=en or ?lang=en_US (deprecated)
  3. look for a currently set ‘ofw_locale’ cookie value (which is actually locale)
  4. look for a currently set ‘OFW_LOCALE’ Apache server variable
  5. look for the subdomain and then the top level domain to see if it matches any of the available locales
  6. if nothing is found, it is set to the default locale

It will save the resulting value in the ofw_locale cookie so that future page loads will remain in the selected locale even if the query string is not given.

You can additionally specify the disable_locale_cookie parameter in the query string like ?locale=en_US&disable_locale_cookie=yes. This will prevent a cookie from being saved. This is useful when you want to display a single page in a selected language but not change the whole site’s locale.

Set the locale by subdomain or tld

You can also add custom logic to set a locale by subdomain:

Or set a locale by top level domain (TLD):

Of course, you can use any other custom logic to set the current language – it does not have to be automatic or based on domain.

Adding locale support to Linux

In order for locales to work perfectly, you’ll need to add locale support system wide. On Ubuntu/Debian systems, you can use the following commands:

locale-gen hu_HU.UTF-8
dpkg-reconfigure locales

Obviously you should substitute hu_HU.UTF-8 for whichever locale you wish to install. Here’s some more info on Ubuntu’s site.

Outlast Web & Mobile Development (c) 2023 | Privacy Policy |