PHP's i18n_loc_set_default in JavaScript

How to use

You you can install via yarn add locutus and require this function via const i18n_loc_set_default = require('locutus/php/i18n/i18n_loc_set_default').

It is important to use a bundler that supports tree-shaking so that you only ship the functions that you actually use to your browser, instead of all of Locutus, which is massive. Examples are: Parcel, webpack, or rollup.js. For server-side use this is typically less of a concern.

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_set_default('pt_PT')true

Notes

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

module.exports = function i18n_loc_set_default(name) {
// discuss at: https://locutus.io/php/i18n_loc_set_default/
// original by: Brett Zamir (https://brett-zamir.me)
// note 1: Renamed in PHP6 from locale_set_default(). Not listed yet at php.net
// note 1: List of locales at https://demo.icu-project.org/icu-bin/locexp (use for implementing other locales here)
// note 1: To be usable with sort() if it is passed the SORT_LOCALE_STRING sorting flag: https://php.net/manual/en/function.sort.php
// 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
}

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we assimilate first iterations with imperfections, hoping for others to take issue with-and improve them. This unorthodox approach has worked very well to foster fun and fruitful collaboration, but please be reminded to use our creations at your own risk. THE SOFTWARE IS PROVIDED "AS IS" has never been more true than for Locutus.

Now go and: [ View on GitHub | Edit on GitHub | View Raw ]


« More PHP i18n functions


Star