1
1
import { CrowdinLocale } from './constants' ;
2
- import type { I18nMethods } from './I18nMethods' ;
3
2
import { pluralsDictionary , simpleDictionary } from './locales' ;
4
3
5
4
type SimpleDictionary = typeof simpleDictionary ;
6
5
type PluralDictionary = typeof pluralsDictionary ;
7
6
8
- export type SimpleLocalizerTokens = keyof SimpleDictionary ;
7
+ type SimpleLocalizerTokens = keyof SimpleDictionary ;
9
8
type PluralLocalizerTokens = keyof PluralDictionary ;
10
9
11
10
export type MergedLocalizerTokens = SimpleLocalizerTokens | PluralLocalizerTokens ;
@@ -56,11 +55,11 @@ function log(message: Parameters<Logger>[0]) {
56
55
logger ( message ) ;
57
56
}
58
57
59
- export function isSimpleToken ( token : string ) : token is SimpleLocalizerTokens {
58
+ function isSimpleToken ( token : string ) : token is SimpleLocalizerTokens {
60
59
return token in simpleDictionary ;
61
60
}
62
61
63
- export function isPluralToken ( token : string ) : token is PluralLocalizerTokens {
62
+ function isPluralToken ( token : string ) : token is PluralLocalizerTokens {
64
63
return token in pluralsDictionary ;
65
64
}
66
65
@@ -73,17 +72,8 @@ type TokenWithArgs<Dict> = {
73
72
74
73
type MergedTokenWithArgs = TokenWithArgs < SimpleDictionary > | TokenWithArgs < PluralDictionary > ;
75
74
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
-
83
75
type DynamicArgStr = 'string' | 'number' ;
84
76
85
- export type LocalizerDictionary = SimpleDictionary ;
86
-
87
77
type ArgsTypeStrToTypes < T extends DynamicArgStr > = T extends 'string'
88
78
? string
89
79
: T extends 'number'
@@ -128,62 +118,19 @@ type MappedToTsTypes<T extends Record<string, DynamicArgStr>> = {
128
118
[ K in keyof T ] : ArgsTypeStrToTypes < T [ K ] > ;
129
119
} ;
130
120
131
- export function propsToTuple < T extends MergedLocalizerTokens > (
121
+ export function strippedWithObj < T extends MergedLocalizerTokens > (
132
122
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 >
171
123
) : string | T {
172
- const sanitizedArgs = args ? sanitizeArgs ( args , '\u200B' ) : undefined ;
124
+ const sanitizedArgs = opts . args ? sanitizeArgs ( opts . args , '\u200B' ) : undefined ;
173
125
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 ( ) ;
177
128
178
129
const strippedString = i18nString . replaceAll ( / < [ ^ > ] * > / g, '' ) ;
179
130
180
131
return deSanitizeHtmlTags ( strippedString , '\u200B' ) ;
181
132
}
182
133
183
- export const strippedWithObj : I18nMethods [ 'strippedWithObj' ] = opts => {
184
- return stripped ( ...propsToTuple ( opts ) ) ;
185
- } ;
186
-
187
134
/**
188
135
* Sanitizes the args to be used in the i18n function
189
136
* @param args The args to sanitize
@@ -213,14 +160,17 @@ export function sanitizeArgs(
213
160
* @deprecated
214
161
*
215
162
*/
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 {
217
167
/** Find and replace the dynamic variables in a localized string and substitute the variables with the provided values */
218
168
return rawMessage . replace ( / \{ ( \w + ) \} / g, ( match : any , arg : string ) => {
219
169
const matchedArg = args ? args [ arg as keyof typeof args ] : undefined ;
220
170
221
171
return matchedArg ?. toString ( ) ?? match ;
222
172
} ) ;
223
- } ;
173
+ }
224
174
225
175
/**
226
176
* 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
231
181
*
232
182
* NOTE: This is intended to be used to get the raw string then format it with {@link formatMessageWithArgs}
233
183
*/
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 {
235
188
try {
236
189
if (
237
190
typeof window !== 'undefined' &&
@@ -275,7 +228,7 @@ export const getRawMessage: I18nMethods['getRawMessage'] = (crowdinLocale, ...[t
275
228
log ( error . message ) ;
276
229
return token ;
277
230
}
278
- } ;
231
+ }
279
232
280
233
function getStringForRule ( {
281
234
dictionary,
@@ -489,7 +442,7 @@ export function localize<T extends MergedLocalizerTokens>(token: T) {
489
442
return new LocalizedStringBuilder < T > ( token , localeInUse ) ;
490
443
}
491
444
492
- export function localizeFromOld < T extends MergedLocalizerTokens > ( token : T , args : ArgsFromToken < T > ) {
445
+ function localizeFromOld < T extends MergedLocalizerTokens > ( token : T , args : ArgsFromToken < T > ) {
493
446
return localize ( token ) . withArgs ( args ) ;
494
447
}
495
448
0 commit comments