forked from allyoucanmap/geonode-mapstore-client
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathLocaleUtils.js
More file actions
62 lines (57 loc) · 1.95 KB
/
LocaleUtils.js
File metadata and controls
62 lines (57 loc) · 1.95 KB
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
/*
* Copyright 2026, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* @module utils/LocaleUtils
*/
/**
* Normalizes a locale code to a standard format (language-region).
* If the locale code has a region, it returns the language-region combination.
* Otherwise, it maximizes the locale to include region information.
*
* @param {string} code - The locale code to normalize (e.g., 'en', 'en-US', 'it')
* @returns {string} The normalized locale code in language-region format, or an empty string if normalization fails
*
* @example
* longLocale('en'); // Returns 'en-US' (or similar based on system locale)
* longLocale('en-GB'); // Returns 'en-GB'
* longLocale('invalid code'); // Returns ''
*
* shortLocale('en-US'); // Returns 'en'
* shortLocale('it-IT'); // Returns 'it'
* shortLocale('invalid code'); // Returns ''
*/
/**
* return the normalized locale code in language-region format, 5 chars length e.g. 'en-US', 'it-IT'
* @param {*} code
* @returns {string} The normalized locale code in language-region format, or an empty string if normalization fails
*/
export function longLocale(code) {
if (!code) return '';
try {
const loc = new Intl.Locale(code);
if (loc.region) return `${loc.language}-${loc.region}`;
const maximized = loc.maximize();
return `${maximized.language}-${maximized.region}`;
} catch {
return '';
}
}
/**
* return the language component of a locale code, or an empty string if shortening fails, length is 2 chars e.g. 'en', 'it'
* @param {string} code - The locale code to shorten (e.g., 'en-US', 'it-IT')
* @returns {string} The language component of the locale code, or an empty string if shortening fails
*/
export function shortLocale(code) {
if (!code) return '';
try {
const loc = new Intl.Locale(code);
return loc.language;
} catch {
return '';
}
}