PHP's i18n_loc_set_default in JavaScript

Here’s what our current JavaScript equivalent to PHP's i18n_loc_set_default looks like.

module.exports = function i18n_loc_set_default (name) { // eslint-disable-line camelcase
// discuss at:
// original by: Brett Zamir (
// note 1: Renamed in PHP6 from locale_set_default(). Not listed yet at
// note 1: List of locales at (use for implementing other locales here)
// note 1: To be usable with sort() if it is passed the SORT_LOCALE_STRING sorting flag:
// example 1: i18n_loc_set_default('pt_PT')
// returns 1: true
const $global = (typeof window !== 'undefined' ? window : global)
$global.$locutus = $global.$locutus || {}
const $locutus = $global.$locutus
$locutus.php = $locutus.php || {}
$locutus.php.locales = $locutus.php.locales || {}
$locutus.php.locales.en_US_POSIX = {
sorting: function (str1, str2) {
// @todo: This one taken from strcmp, but need for other locales;
// we don't use localeCompare since its locale is not settable
return (str1 === str2) ? 0 : ((str1 > str2) ? 1 : -1)
$locutus.php.locale_default = name
return true
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/i18n/i18n_loc_set_default'). You could also require the i18n module in full so that you could access i18n.i18n_loc_set_default instead.

If you intend to target the browser, you can then use a module bundler such as Parcel, webpack, Browserify, or rollup.js. This can be important because Locutus allows modern JavaScript in the source files, meaning it may not work in all browsers without a build/transpile step. Locutus does transpile all functions to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.



Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result

« More PHP i18n functions