-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Intl.Locale internally #605
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,32 @@ | ||
/* eslint no-magic-numbers: 0 */ | ||
function convertMasks(locale: string): string { | ||
let result; | ||
if (locale[0] === "*") { | ||
result = "und" + locale.substr(1); | ||
} else { | ||
result = locale; | ||
}; | ||
return result.replace(/\-\*/g, ""); | ||
} | ||
|
||
const languageCodeRe = "([a-z]{2,3}|\\*)"; | ||
const scriptCodeRe = "(?:-([a-z]{4}|\\*))"; | ||
const regionCodeRe = "(?:-([a-z]{2}|\\*))"; | ||
const variantCodeRe = "(?:-(([0-9][a-z0-9]{3}|[a-z0-9]{5,8})|\\*))"; | ||
function getVisibleLangTagLength(language: any, script: any, region: any) { | ||
let result = 0; | ||
result += language ? language.length : "und".length; | ||
result += script ? script.length + 1 : 0; | ||
result += region ? region.length + 1 : 0; | ||
return result; | ||
} | ||
|
||
/** | ||
* Regular expression splitting locale id into four pieces: | ||
* | ||
* Example: `en-Latn-US-macos` | ||
* | ||
* language: en | ||
* script: Latn | ||
* region: US | ||
* variant: macos | ||
* | ||
* It can also accept a range `*` character on any position. | ||
*/ | ||
const localeRe = new RegExp( | ||
`^${languageCodeRe}${scriptCodeRe}?${regionCodeRe}?${variantCodeRe}?$`, | ||
"i" | ||
); | ||
function getExtensionStart(locale: string): number | undefined { | ||
let pos = locale.search(/-[a-zA-Z]-/); | ||
if (pos === -1) { | ||
return undefined; | ||
} | ||
return pos; | ||
} | ||
|
||
export class Locale { | ||
isWellFormed: boolean; | ||
language?: string; | ||
language: string; | ||
script?: string; | ||
region?: string; | ||
variant?: string; | ||
|
@@ -39,29 +41,55 @@ export class Locale { | |
* properly parsed as `en-*-US-*`. | ||
*/ | ||
constructor(locale: string) { | ||
const result = localeRe.exec(locale.replace(/_/g, "-")); | ||
if (!result) { | ||
let result; | ||
let normalized = convertMasks(locale.replace(/_/g, "-")); | ||
try { | ||
result = new Intl.Locale(normalized); | ||
} catch (e) { | ||
this.isWellFormed = false; | ||
this.language = "und"; | ||
return; | ||
} | ||
|
||
let [, language, script, region, variant] = result; | ||
this.language = result.language || "und"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How could |
||
this.script = result.script; | ||
this.region = result.region; | ||
|
||
if (language) { | ||
this.language = language.toLowerCase(); | ||
let visiblelangTagLength = getVisibleLangTagLength(this.language, this.script, this.region); | ||
|
||
if (normalized.length > visiblelangTagLength) { | ||
let extStart = getExtensionStart(locale); | ||
this.variant = locale.substring(visiblelangTagLength + 1, extStart); | ||
} | ||
|
||
this.isWellFormed = true; | ||
} | ||
|
||
static fromComponents({language, script, region, variant}: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could/should this also use |
||
language?: string, | ||
script?: string, | ||
region?: string, | ||
variant?: string, | ||
}): Locale { | ||
let result = new Locale("und"); | ||
if (language && language !== "*") { | ||
result.language = language; | ||
} | ||
if (script) { | ||
this.script = script[0].toUpperCase() + script.slice(1); | ||
if (script && script !== "*") { | ||
result.script = script; | ||
} | ||
if (region) { | ||
this.region = region.toUpperCase(); | ||
if (region && region !== "*") { | ||
result.region = region; | ||
} | ||
this.variant = variant; | ||
this.isWellFormed = true; | ||
if (variant && variant !== "*") { | ||
result.variant = variant; | ||
} | ||
return result; | ||
} | ||
|
||
isEqual(other: Locale): boolean { | ||
return ( | ||
this.isWellFormed === other.isWellFormed && | ||
this.language === other.language && | ||
this.script === other.script && | ||
this.region === other.region && | ||
|
@@ -71,9 +99,10 @@ export class Locale { | |
|
||
matches(other: Locale, thisRange = false, otherRange = false): boolean { | ||
return ( | ||
this.isWellFormed && other.isWellFormed && | ||
(this.language === other.language || | ||
(thisRange && this.language === undefined) || | ||
(otherRange && other.language === undefined)) && | ||
(thisRange && this.language === "und") || | ||
(otherRange && other.language === "und")) && | ||
(this.script === other.script || | ||
(thisRange && this.script === undefined) || | ||
(otherRange && other.script === undefined)) && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"target": "es2020", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this enough? I was under the impression that TS only added the |
||
"outDir": "./esm" | ||
}, | ||
"include": ["./src/**/*.ts"] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should really be typed as
string | undefined
; there should be no need forany
here.