Languages::setLocale() method

Set the current locale

This function behaves exactly the same way as PHP setlocale except for the following:

  • If the $locale argument is omitted, it uses the locale setting translated for the current user language.
  • You can optionally specify a CSV string of locales to try for the $locale argument.
  • You can optionally or a “category=locale;category=locale;category=locale” string for the $locale argument. When this type of string is used, the $category argument is ignored.
  • This method does not accept more than the 3 indicated arguments.
  • Any of the arguments may be swapped.

See the PHP setlocale link above for a list of constants that can be used for the $category argument.

Note that the locale is set once at bootup by ProcessWire, and does not change after that unless you call this method. Meaning, a change to $user->language does not automatically change the locale. If you want to change the locale, you would have to call this method after changing the user’s language from the API side.

Example

// Set locale to whatever settings defined for current $user language
$languages->setLocale();

// Set all locale categories
$languages->setLocale(LC_ALL, 'en_US.UTF-8');

// Set locale for specific category (CTYPE)
$langauges->setLocale(LC_CTYPE, 'en_US.UTF-8');

// Try multiple locales till one works (in order) using array
$languages->setLocale(LC_ALL, [ 'en_US.UTF-8', 'en_US', 'en' ]);

// Same as above, except using CSV string
$languages->setLocale(LC_ALL, 'en_US.UTF-8, en_US, en');

// Set multiple categories and locales (first argument ignored)
$languages->setLocale(null, 'LC_CTYPE=en_US;LC_NUMERIC=de_DE;LC_TIME=es_ES'); 

Usage

// basic usage
$string = $languages->setLocale();

// usage with all arguments
$string = $languages->setLocale($category = 6, $locale = null);

Arguments

NameType(s)Description
category (optional)int, string, array, null, Language

Specify a PHP “LC_” constant (int) or omit (or null) for default (LC_ALL).

locale (optional)int, string, array, null, Language

Specify string, array or CSV string of locale name(s), omit (null) for current language locale, or specify Language object to pull locale from that language.

Return value

string bool

Returns the locale that was set or boolean false if requested locale cannot be set.

See Also


Languages methods and properties

API reference based on ProcessWire core version 3.0.137

Twitter updates

  • ProcessWire 3.0.142 has a lot of updates but the biggest is the addition of custom fields support for file and image fields. In this post, we take a closer look at that, and also outline all of the new features in the just-released FormBuilder v40— More
    11 October 2019
  • This week’s dev branch version brings you improvements to ProcessWire’s input->cookie API variable, plus it adds the ability to modify system URLs and paths at runtime. This post also includes some examples to demonstrate just how useful this can be— More
    20 September 2019
  • New post: This week we’ll take a look at a new version of FormBuilder that's on the way (with a screencast), as well as the latest version of the core: ProcessWire 3.0.140— More
    6 September 2019

Latest news

  • ProcessWire Weekly #283
    The 283rd issue of ProcessWire Weekly introduces ProcessWire 3.0.142 and FormBuilder v40, highlights some conference content, and features a new weekly poll continuing with the #processwireworks theme. Read on!
    Weekly.pw / 12 October 2019
  • ProcessWire 3.0.142 core updates
    This latest version of the core on the dev branch has a lot of updates, bnd the biggest is the addition of custom fields support for file and image fields. In this post, we take a closer look and also outline all of the new features in the just-released FormBuilder v40.
    Blog / 11 October 2019
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits