Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Feb 18, 2024
1 parent a485318 commit fe5e78f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
34 changes: 19 additions & 15 deletions src/lib/internationalization/internationalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { ReflectionKind } from "../models/reflections/kind";
* // Define a translatable string with no arguments
* plugin_msg: [];
* // Define a translatable string requiring one argument
* plugin_msg_1: [string];
* plugin_msg_0: [string];
* }
* }
* ```
Expand Down Expand Up @@ -74,11 +74,12 @@ export class Internationalization {
(lang) => {
// Make sure this isn't abused to load some random file by mistake
ok(
/^[A-Za-z\-]+$/.test(lang),
/^[A-Za-z-]+$/.test(lang),
"Locale names may only contain letters and dashes",
);
try {
return new Map(
// eslint-disable-next-line @typescript-eslint/no-var-requires
Object.entries(require(`./locales/${lang}.${ext}`)),
);
} catch {
Expand All @@ -87,6 +88,22 @@ export class Internationalization {
},
);

/**
* Proxy object which supports dynamically translating
* all supported keys. This is generally used rather than the translate
* method so that renaming a key on the `translatable` object that contains
* all of the default translations will automatically update usage locations.
*/
proxy: TranslationProxy = new Proxy(this, {
get(internationalization, key) {
return (...args: string[]) =>
internationalization.translate(
key as never,
...(args as never),
);
},
}) as never as TranslationProxy;

/**
* If constructed without an application, will use the default language.
* Intended for use in unit tests only.
Expand Down Expand Up @@ -247,17 +264,4 @@ export class Internationalization {
...this.allTranslations.keys(),
]).sort();
}

/**
* Creates a proxy object which supports dynamically translating
* all supported keys. This is generally used rather than the translate
* method so that renaming a key on the `translatable` object that contains
* all of the default translations will automatically update usage locations.
*/
proxy: TranslationProxy = new Proxy({} as TranslationProxy, {
get: ({}, key) => {
return (...args: string[]) =>
this.translate(key as never, ...(args as never));
},
});
}
2 changes: 1 addition & 1 deletion src/lib/utils/loggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const messagePrefixes = {
const dummyTranslationProxy: TranslationProxy = new Proxy(
{} as TranslationProxy,
{
get: ({}, key) => {
get: (_target, key) => {
return (...args: string[]) =>
String(key).replace(/\{(\d+)\}/g, (_, index) => {
return args[+index] ?? "(no placeholder)";
Expand Down
1 change: 1 addition & 0 deletions src/test/internationalization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe("Locales", () => {

for (const locale of readdirSync(localeRoot)) {
it(`${locale} defines a valid locale`, () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const translations = require(join(localeRoot, locale)) as Record<
string,
string
Expand Down

0 comments on commit fe5e78f

Please sign in to comment.