-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make getLocales function simpler
- Loading branch information
Showing
14 changed files
with
116 additions
and
116 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,8 @@ | ||
{ | ||
"useTabs": false, | ||
"printWidth": 800, | ||
"tabWidth": 2, | ||
"singleQuote": true, | ||
"trailingComma": "es5", | ||
"semi": true | ||
} |
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
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 |
---|---|---|
@@ -1,25 +1,33 @@ | ||
import getAllLocales from './util/getAllLocales.js' | ||
import stringToJsonKey from './util/stringToJsonKey.js' | ||
import { InterpolationObject, LocalizationOptions } from './util/types' | ||
import getLocales from './util/getLocales'; | ||
import { Interpolations, LocalizationOptions } from './util/types'; | ||
|
||
export class Localization { | ||
private initOptions: LocalizationOptions | ||
private initLocales: LocalizationOptions['locales'] | ||
private initOptions: LocalizationOptions; | ||
private initLocales: LocalizationOptions['locales']; | ||
|
||
constructor(options: LocalizationOptions) { | ||
this.initOptions = options | ||
this.initLocales = JSON.parse(JSON.stringify(options.locales)) | ||
} | ||
constructor(options: LocalizationOptions) { | ||
this.initOptions = options; | ||
this.initLocales = JSON.parse(JSON.stringify(options.locales)); | ||
} | ||
|
||
t(key: string, interp?: InterpolationObject, lang = this.initOptions.defaultLocale) { | ||
return stringToJsonKey(key, this.initLocales, lang, this.initOptions.fallbackLocale, interp) | ||
} | ||
t(key: string, interp?: Interpolations, lang = this.initOptions.defaultLocale) { | ||
return getLocales({ | ||
key, | ||
locales: this.initLocales, | ||
defaultLang: lang, | ||
fallbackLang: this.initOptions.fallbackLocale, | ||
interpolations: interp, | ||
}) as string; | ||
} | ||
|
||
localizationFor(key: string) { | ||
return getAllLocales(key, this.initOptions.locales) | ||
} | ||
localizationFor(key: string) { | ||
return getLocales({ | ||
key, | ||
locales: this.initLocales, | ||
}) as Record<string, string>; | ||
} | ||
|
||
changeLanguage(lang: string) { | ||
this.initOptions.defaultLocale = lang | ||
} | ||
changeLanguage(lang: string) { | ||
this.initOptions.defaultLocale = lang; | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
import { interpolate } from "./interpolate"; | ||
import { Interpolations, LocaleObject, Locales } from "./types"; | ||
|
||
export default function getLocales(args: Args): string | Record<string, string> { | ||
const { key, locales, defaultLang, fallbackLang, interpolations } = args; | ||
const translations: Record<string, string> = {}; | ||
|
||
for (const lang in locales) { | ||
// Navigate through nested keys | ||
let value = locales[lang]; | ||
for (const k of key.split(".")) { | ||
value = value[k] as LocaleObject; | ||
if (!value) break; | ||
} | ||
|
||
// Handle both string values and object values | ||
if (value) { | ||
if (typeof value === "object") { | ||
const innerKey = Object.keys(value)[0]; | ||
const interpolatedKey = interpolate(innerKey, interpolations); | ||
translations[lang] = value[interpolatedKey] as string; | ||
} else if (typeof value === "string") { | ||
translations[lang] = interpolate(value, interpolations); | ||
} | ||
} | ||
} | ||
|
||
// If defaultLang is provided, return single translation with fallback | ||
if (defaultLang) { | ||
return translations[defaultLang] || translations[fallbackLang!] || key; | ||
} | ||
|
||
// Otherwise return all translations | ||
return translations; | ||
} | ||
|
||
type Args = { | ||
key: string, | ||
locales: Locales, | ||
defaultLang?: string, | ||
fallbackLang?: string, | ||
interpolations?: Interpolations | ||
} |
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,12 @@ | ||
import sanitize from './sanitize'; | ||
import { Interpolations } from './types'; | ||
|
||
export function interpolate(text: string, interpolations?: Interpolations) { | ||
if (!interpolations) return text; | ||
|
||
for (const key in interpolations) { | ||
// sanitize the key to prevent XSS | ||
text = text.replaceAll(`{{${key}}}`, sanitize(interpolations[key] as string)); | ||
} | ||
return text; | ||
} |
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
export default function sanitize(text: string): string { | ||
return text.replace( | ||
/[&<>]/g, | ||
(char) => | ||
({ | ||
"&": "&", | ||
"<": "<", | ||
">": ">", | ||
})[char] || char, | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,32 +1,17 @@ | ||
// json types | ||
export type JSONValue = | ||
| string | ||
| number | ||
| boolean | ||
| JSONObject | ||
| JSONArray; | ||
|
||
export interface JSONObject { | ||
[x: string]: JSONValue; | ||
} | ||
|
||
export interface JSONArray extends Array<JSONValue> { } | ||
|
||
export interface LocalizationOptions { | ||
locales: Locales; | ||
defaultLocale: string; | ||
fallbackLocale: string; | ||
} | ||
|
||
export interface InterpolationObject { | ||
[key: string]: string | (() => boolean) | undefined; | ||
pluralChecker?: string | (() => boolean); | ||
} | ||
|
||
export type LocaleObject = { | ||
[key: string]: string | LocaleObject; | ||
}; | ||
|
||
export type Locales = { | ||
[lang: string]: LocaleObject; | ||
}; | ||
}; | ||
|
||
export interface LocalizationOptions { | ||
locales: Locales; | ||
defaultLocale: string; | ||
fallbackLocale: string; | ||
} | ||
|
||
export type Interpolations = { | ||
[key: string]: string; | ||
}; |
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
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