PHP's i18n_loc_get_default in JavaScript

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module.exports = function i18n_loc_get_default () { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/i18n_loc_get_default/
// original by: Brett Zamir (http://brett-zamir.me)
// note 1: Renamed in PHP6 from locale_get_default(). Not listed yet at php.net.
// note 1: List of locales at <http://demo.icu-project.org/icu-bin/locexp>
// note 1: To be usable with sort() if it is passed the `SORT_LOCALE_STRING`
// note 1: sorting flag: http://php.net/manual/en/function.sort.php
// example 1: i18n_loc_get_default()
// returns 1: 'en_US_POSIX'
// example 2: i18n_loc_set_default('pt_PT')
// example 2: i18n_loc_get_default()
// returns 2: 'pt_PT'

var $global = (typeof window !== 'undefined' ? window : global)
$global.$locutus = $global.$locutus || {}
var $locutus = $global.$locutus
$locutus.php = $locutus.php || {}
$locutus.php.locales = $locutus.php.locales || {}

return $locutus.php.locale_default || 'en_US_POSIX'
}
[ 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_get_default'). You could also require the i18n module in full so that you could access i18n.i18n_loc_get_default instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles 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.

Notes

Examples

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
1i18n_loc_get_default()'en_US_POSIX'
2i18n_loc_set_default('pt_PT') i18n_loc_get_default()'pt_PT'

« More PHP i18n functions