Skip to content

Commit 2bf00c4

Browse files
committed
chore: finished remove i18nMethods wrapped calls
1 parent 996a718 commit 2bf00c4

File tree

25 files changed

+52
-215
lines changed

25 files changed

+52
-215
lines changed

about_preload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const { initializeRendererProcessLogger } = require('./ts/util/logger/renderer_p
1616

1717
initializeRendererProcessLogger();
1818

19-
window.i18n = setupI18n({
19+
setupI18n({
2020
crowdinLocale,
2121
});
2222

password_preload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const { initializeRendererProcessLogger } = require('./ts/util/logger/renderer_p
1717

1818
initializeRendererProcessLogger();
1919

20-
window.i18n = setupI18n({
20+
setupI18n({
2121
crowdinLocale,
2222
});
2323

preload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const { initializeRendererProcessLogger } = require('./ts/util/logger/renderer_p
2323

2424
initializeRendererProcessLogger();
2525

26-
window.i18n = setupI18n({ crowdinLocale });
26+
setupI18n({ crowdinLocale });
2727

2828
let title = config.name;
2929
if (config.environment !== 'production') {

tools/localization/regex.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ def get_localization_regex_list(string):
1111
fr"\{{ token: '{key}'(, args: {{.*}})? \}}",
1212
# This also captures the same group as `basic_object` but this is fine because basic_object shortcuts before reaching here if found.
1313
fr"{{\s+token: '{key}',?\s+(\s*args: {{[\S\s.]*}},)?\s+\}}",
14-
fr"window\.i18n\.(getRawMessage)\('{key}'(, {{[\S\s.]*}})?\)",
1514
fr"<I18n[\S\s.]*token=\{{?['\"]{key}['\"]\}}?",
1615
fr"<I18n[\S\s.]*token=[\S\s.]*{key}[\S\s.]*",
1716
fr"i18n\('{key}'\)",
1817
fr"i18n\('{key}'(, {{[\S\s.]*}})?\)",
19-
fr"i18n\.(getRawMessage)\('{key}'(, {{[\S\s.]*}})?\)",
2018
fr"window\?\.i18n\?\.\('{key}'(, {{[\S\s.]*}})?\)"
2119
]
2220

ts/components/basic/Localizer.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import styled from 'styled-components';
22
import { SessionHtmlRenderer } from './SessionHTMLRenderer';
33
import {
4+
formatMessageWithArgs,
45
GetMessageArgs,
56
isArgsFromTokenWithIcon,
67
MergedLocalizerTokens,
78
sanitizeArgs,
9+
getRawMessage,
810
type LocalizerComponentProps,
911
} from '../../localization/localeTools';
1012
import { getCrowdinLocale } from '../../util/i18n/shared';
@@ -49,7 +51,7 @@ export const Localizer = <T extends MergedLocalizerTokens>(
4951
) => {
5052
const args = 'args' in props ? props.args : undefined;
5153

52-
let rawString: string = window.i18n.getRawMessage<T>(
54+
let rawString: string = getRawMessage<T>(
5355
getCrowdinLocale(),
5456
...([props.token, args] as GetMessageArgs<T>)
5557
);
@@ -67,10 +69,7 @@ export const Localizer = <T extends MergedLocalizerTokens>(
6769
rawString = rawString.replaceAll(/\{icon}/g, `<span role='img'>{icon}</span>`);
6870
}
6971

70-
const i18nString = window.i18n.formatMessageWithArgs(
71-
rawString,
72-
cleanArgs as GetMessageArgs<T>[1]
73-
);
72+
const i18nString = formatMessageWithArgs(rawString, cleanArgs as GetMessageArgs<T>[1]);
7473

7574
return containsFormattingTags || containsIcons ? (
7675
/** If the string contains a relevant formatting tag, render it as HTML */

ts/components/conversation/message/message-content/MessageText.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const StyledMessageText = styled.div`
2626

2727
const StyledMessageDeleted = styled.div`
2828
display: flex;
29-
flexgap: var(--margins-xs);
29+
gap: var(--margins-xs);
3030
flex-direction: row;
3131
align-items: center;
3232
`;

ts/localization/I18nMethods.d.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

ts/localization/localeTools.ts

Lines changed: 18 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { CrowdinLocale } from './constants';
2-
import type { I18nMethods } from './I18nMethods';
32
import { pluralsDictionary, simpleDictionary } from './locales';
43

54
type SimpleDictionary = typeof simpleDictionary;
65
type PluralDictionary = typeof pluralsDictionary;
76

8-
export type SimpleLocalizerTokens = keyof SimpleDictionary;
7+
type SimpleLocalizerTokens = keyof SimpleDictionary;
98
type PluralLocalizerTokens = keyof PluralDictionary;
109

1110
export type MergedLocalizerTokens = SimpleLocalizerTokens | PluralLocalizerTokens;
@@ -56,11 +55,11 @@ function log(message: Parameters<Logger>[0]) {
5655
logger(message);
5756
}
5857

59-
export function isSimpleToken(token: string): token is SimpleLocalizerTokens {
58+
function isSimpleToken(token: string): token is SimpleLocalizerTokens {
6059
return token in simpleDictionary;
6160
}
6261

63-
export function isPluralToken(token: string): token is PluralLocalizerTokens {
62+
function isPluralToken(token: string): token is PluralLocalizerTokens {
6463
return token in pluralsDictionary;
6564
}
6665

@@ -73,17 +72,8 @@ type TokenWithArgs<Dict> = {
7372

7473
type MergedTokenWithArgs = TokenWithArgs<SimpleDictionary> | TokenWithArgs<PluralDictionary>;
7574

76-
export function isTokenWithArgs(token: string): token is MergedTokenWithArgs {
77-
return (
78-
(isSimpleToken(token) && !isEmptyObject(simpleDictionary[token]?.args)) ||
79-
(isPluralToken(token) && !isEmptyObject(pluralsDictionary[token]?.args))
80-
);
81-
}
82-
8375
type DynamicArgStr = 'string' | 'number';
8476

85-
export type LocalizerDictionary = SimpleDictionary;
86-
8777
type ArgsTypeStrToTypes<T extends DynamicArgStr> = T extends 'string'
8878
? string
8979
: T extends 'number'
@@ -128,62 +118,19 @@ type MappedToTsTypes<T extends Record<string, DynamicArgStr>> = {
128118
[K in keyof T]: ArgsTypeStrToTypes<T[K]>;
129119
};
130120

131-
export function propsToTuple<T extends MergedLocalizerTokens>(
121+
export function strippedWithObj<T extends MergedLocalizerTokens>(
132122
opts: LocalizerComponentProps<T, string>
133-
): GetMessageArgs<T> {
134-
return (
135-
isTokenWithArgs(opts.token) ? [opts.token, opts.args] : [opts.token]
136-
) as GetMessageArgs<T>;
137-
}
138-
139-
/**
140-
* Retrieves a localized message string, substituting variables where necessary.
141-
*
142-
* @param token - The token identifying the message to retrieve.
143-
* @param args - An optional record of substitution variables and their replacement values. This is required if the string has dynamic variables.
144-
*
145-
* @returns The localized message string with substitutions applied.
146-
*/
147-
function getMessageDefault<T extends MergedLocalizerTokens>(
148-
...props: GetMessageArgs<T>
149-
): string {
150-
const token = props[0];
151-
try {
152-
return localizeFromOld(props[0], props[1] as ArgsFromToken<T>).toString();
153-
} catch (error) {
154-
log(error.message);
155-
return token;
156-
}
157-
}
158-
159-
/**
160-
* Retrieves a localized message string, substituting variables where necessary. Then strips the message of any HTML and custom tags.
161-
*
162-
* @deprecated This will eventually be replaced altogether by LocalizedStringBuilder
163-
*
164-
* @param token - The token identifying the message to retrieve.
165-
* @param args - An optional record of substitution variables and their replacement values. This is required if the string has dynamic variables.
166-
*
167-
* @returns The localized message string with substitutions applied. Any HTML and custom tags are removed.
168-
*/
169-
function stripped<T extends MergedLocalizerTokens>(
170-
...[token, args]: GetMessageArgs<T>
171123
): string | T {
172-
const sanitizedArgs = args ? sanitizeArgs(args, '\u200B') : undefined;
124+
const sanitizedArgs = opts.args ? sanitizeArgs(opts.args, '\u200B') : undefined;
173125

174-
// Note: the `as any` is needed because we don't have the <T> template argument available
175-
// when enforcing the type of the stripped function to be the one defined by I18nMethods
176-
const i18nString = getMessageDefault(...([token, sanitizedArgs] as GetMessageArgs<any>));
126+
// Note: the `as any` is needed sanitizeArgs does not preserve types
127+
const i18nString = localizeFromOld(opts.token, sanitizedArgs as any).toString();
177128

178129
const strippedString = i18nString.replaceAll(/<[^>]*>/g, '');
179130

180131
return deSanitizeHtmlTags(strippedString, '\u200B');
181132
}
182133

183-
export const strippedWithObj: I18nMethods['strippedWithObj'] = opts => {
184-
return stripped(...propsToTuple(opts));
185-
};
186-
187134
/**
188135
* Sanitizes the args to be used in the i18n function
189136
* @param args The args to sanitize
@@ -213,14 +160,17 @@ export function sanitizeArgs(
213160
* @deprecated
214161
*
215162
*/
216-
export const formatMessageWithArgs: I18nMethods['formatMessageWithArgs'] = (rawMessage, args) => {
163+
export function formatMessageWithArgs<T extends MergedLocalizerTokens>(
164+
rawMessage: string,
165+
args?: ArgsFromToken<T>
166+
): string | T {
217167
/** Find and replace the dynamic variables in a localized string and substitute the variables with the provided values */
218168
return rawMessage.replace(/\{(\w+)\}/g, (match: any, arg: string) => {
219169
const matchedArg = args ? args[arg as keyof typeof args] : undefined;
220170

221171
return matchedArg?.toString() ?? match;
222172
});
223-
};
173+
}
224174

225175
/**
226176
* Retrieves a localized message string, without substituting any variables. This resolves any plural forms using the given args
@@ -231,7 +181,10 @@ export const formatMessageWithArgs: I18nMethods['formatMessageWithArgs'] = (rawM
231181
*
232182
* NOTE: This is intended to be used to get the raw string then format it with {@link formatMessageWithArgs}
233183
*/
234-
export const getRawMessage: I18nMethods['getRawMessage'] = (crowdinLocale, ...[token, args]) => {
184+
export function getRawMessage<T extends MergedLocalizerTokens>(
185+
crowdinLocale: CrowdinLocale,
186+
...[token, args]: GetMessageArgs<T>
187+
): string | T {
235188
try {
236189
if (
237190
typeof window !== 'undefined' &&
@@ -275,7 +228,7 @@ export const getRawMessage: I18nMethods['getRawMessage'] = (crowdinLocale, ...[t
275228
log(error.message);
276229
return token;
277230
}
278-
};
231+
}
279232

280233
function getStringForRule({
281234
dictionary,
@@ -489,7 +442,7 @@ export function localize<T extends MergedLocalizerTokens>(token: T) {
489442
return new LocalizedStringBuilder<T>(token, localeInUse);
490443
}
491444

492-
export function localizeFromOld<T extends MergedLocalizerTokens>(token: T, args: ArgsFromToken<T>) {
445+
function localizeFromOld<T extends MergedLocalizerTokens>(token: T, args: ArgsFromToken<T>) {
493446
return localize(token).withArgs(args);
494447
}
495448

ts/models/message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ export class MessageModel extends Model<MessageAttributes> {
381381
timespanSeconds: expireTimer,
382382
});
383383

384-
return window.i18n.strippedWithObj(i18nProps);
384+
return strippedWithObj(i18nProps);
385385
}
386386
const body = this.get('body');
387387
if (body) {

ts/node/locale.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { SetupI18nReturnType } from '../types/localizer';
21
import { setupI18n } from '../util/i18n/i18n';
32
import { CrowdinLocale, isCrowdinLocale } from '../localization/constants';
43

@@ -38,7 +37,6 @@ function resolveLocale(crowdinLocale: string): CrowdinLocale {
3837

3938
export function loadLocalizedDictionary({ appLocale }: { appLocale: string }): {
4039
crowdinLocale: CrowdinLocale;
41-
i18n: SetupI18nReturnType;
4240
} {
4341
if (!appLocale) {
4442
throw new TypeError('`appLocale` is required');
@@ -51,12 +49,11 @@ export function loadLocalizedDictionary({ appLocale }: { appLocale: string }): {
5149
// https://github.com/electron/electron/blob/master/docs/api/locales.md
5250
const crowdinLocale = resolveLocale(appLocale);
5351

54-
const i18n = setupI18n({
52+
setupI18n({
5553
crowdinLocale,
5654
});
5755

5856
return {
5957
crowdinLocale,
60-
i18n,
6158
};
6259
}

0 commit comments

Comments
 (0)