PHP's nl_langinfo in JavaScript

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module.exports = function nl_langinfo (item) { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/nl_langinfo/
// original by: Brett Zamir (http://brett-zamir.me)
// example 1: nl_langinfo('DAY_1')
// returns 1: 'Sunday'

var setlocale = require('../strings/setlocale')

setlocale('LC_ALL', 0) // Ensure locale data is available

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

var loc = $locutus.php.locales[$locutus.php.localeCategories.LC_TIME]
if (item.indexOf('ABDAY_') === 0) {
return loc.LC_TIME.a[parseInt(item.replace(/^ABDAY_/, ''), 10) - 1]
} else if (item.indexOf('DAY_') === 0) {
return loc.LC_TIME.A[parseInt(item.replace(/^DAY_/, ''), 10) - 1]
} else if (item.indexOf('ABMON_') === 0) {
return loc.LC_TIME.b[parseInt(item.replace(/^ABMON_/, ''), 10) - 1]
} else if (item.indexOf('MON_') === 0) {
return loc.LC_TIME.B[parseInt(item.replace(/^MON_/, ''), 10) - 1]
} else {
switch (item) {
// More LC_TIME
case 'AM_STR':
return loc.LC_TIME.p[0]
case 'PM_STR':
return loc.LC_TIME.p[1]
case 'D_T_FMT':
return loc.LC_TIME.c
case 'D_FMT':
return loc.LC_TIME.x
case 'T_FMT':
return loc.LC_TIME.X
case 'T_FMT_AMPM':
return loc.LC_TIME.r
case 'ERA':
case 'ERA_YEAR':
case 'ERA_D_T_FMT':
case 'ERA_D_FMT':
case 'ERA_T_FMT':
// all fall-throughs
return loc.LC_TIME[item]
}
loc = $locutus.php.locales[$locutus.php.localeCategories.LC_MONETARY]
if (item === 'CRNCYSTR') {
// alias
item = 'CURRENCY_SYMBOL'
}
switch (item) {
case 'INT_CURR_SYMBOL':
case 'CURRENCY_SYMBOL':
case 'MON_DECIMAL_POINT':
case 'MON_THOUSANDS_SEP':
case 'POSITIVE_SIGN':
case 'NEGATIVE_SIGN':
case 'INT_FRAC_DIGITS':
case 'FRAC_DIGITS':
case 'P_CS_PRECEDES':
case 'P_SEP_BY_SPACE':
case 'N_CS_PRECEDES':
case 'N_SEP_BY_SPACE':
case 'P_SIGN_POSN':
case 'N_SIGN_POSN':
// all fall-throughs
return loc.LC_MONETARY[item.toLowerCase()]
case 'MON_GROUPING':
// Same as above, or return something different since this returns an array?
return loc.LC_MONETARY[item.toLowerCase()]
}
loc = $locutus.php.locales[$locutus.php.localeCategories.LC_NUMERIC]
switch (item) {
case 'RADIXCHAR':
case 'DECIMAL_POINT':
// Fall-through
return loc.LC_NUMERIC[item.toLowerCase()]
case 'THOUSEP':
case 'THOUSANDS_SEP':
// Fall-through
return loc.LC_NUMERIC[item.toLowerCase()]
case 'GROUPING':
// Same as above, or return something different since this returns an array?
return loc.LC_NUMERIC[item.toLowerCase()]
}
loc = $locutus.php.locales[$locutus.php.localeCategories.LC_MESSAGES]
switch (item) {
case 'YESEXPR':
case 'NOEXPR':
case 'YESSTR':
case 'NOSTR':
// all fall-throughs
return loc.LC_MESSAGES[item]
}
loc = $locutus.php.locales[$locutus.php.localeCategories.LC_CTYPE]
if (item === 'CODESET') {
return loc.LC_CTYPE[item]
}

return false
}
}
[ 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/strings/nl_langinfo'). You could also require the strings module in full so that you could access strings.nl_langinfo 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.

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
1nl_langinfo('DAY_1')'Sunday'

« More PHP strings functions