Skip to content
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

feat(localization): Add en-GB #4313

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-carrots-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/localizations": minor
---

Added support for en-GB localization
1 change: 1 addition & 0 deletions packages/localizations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"da-DK",
"de-DE",
"el-GR",
"en-GB",
"en-US",
"es-ES",
"fi-FI",
Expand Down
54 changes: 54 additions & 0 deletions packages/localizations/src/en-GB.ts
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);
}
Loading