-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(localizations): Add support for en-GB
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@clerk/localizations": minor | ||
--- | ||
|
||
Added support for en-GB localization |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ | |
"da-DK", | ||
"de-DE", | ||
"el-GR", | ||
"en-GB", | ||
"en-US", | ||
"es-ES", | ||
"fi-FI", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* ===================================================================================== | ||
* DISCLAIMER: | ||
* ===================================================================================== | ||
* This localization file is a community contribution and is not officially maintained | ||
* by Clerk. It has been provided by the community and may not be fully aligned | ||
* with the current or future states of the main application. Clerk does not guarantee | ||
* the accuracy, completeness, or timeliness of the translations in this file. | ||
* Use of this file is at your own risk and discretion. | ||
* ===================================================================================== | ||
*/ | ||
|
||
import type { LocalizationResource } from '@clerk/types'; | ||
|
||
import { enUS } from './en-US'; | ||
|
||
export const enGB = translateResource(enUS, { | ||
'en-US': 'en-GB', | ||
authorize: 'authorise', | ||
Authorize: 'Authorise', | ||
organization: 'organisation', | ||
Organization: 'Organisation', | ||
}); | ||
|
||
type Dictionary = Record<string, string>; | ||
|
||
function translateResource(resource: LocalizationResource, dict: Dictionary): typeof resource { | ||
return Object.fromEntries(Object.entries(resource).map(([k, v]) => [k, translateResourceValue(v, dict)])); | ||
} | ||
|
||
type ResourceValue = string | undefined | { [key: string]: ResourceValue }; | ||
|
||
function translateResourceValue(value: ResourceValue, dict: Dictionary): typeof value { | ||
if (value === undefined || value === null) { | ||
return value; | ||
} | ||
if (typeof value === 'string') { | ||
return value | ||
.split(' ') | ||
.map(v => translateWord(v, dict)) | ||
.join(' '); | ||
} | ||
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, translateResourceValue(v, dict)])); | ||
} | ||
|
||
function translateWord(word: string, dict: Dictionary): typeof word { | ||
return isTemplateVariable(word) | ||
? word | ||
: Object.entries(dict).reduce((w, [from, to]) => w.replace(RegExp(from), to), word); | ||
} | ||
|
||
function isTemplateVariable(s: string): boolean { | ||
return /{{.+}}/.test(s); | ||
} |