Skip to content

Commit

Permalink
feat(localizations): Add support for en-GB
Browse files Browse the repository at this point in the history
  • Loading branch information
ijxy committed Oct 25, 2024
1 parent 1b573c6 commit 900336f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
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
58 changes: 58 additions & 0 deletions packages/localizations/src/en-GB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* =====================================================================================
* 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]) => {
return [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);
}

0 comments on commit 900336f

Please sign in to comment.