-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): allow custom interpolation options for i18n (#4727)
* feat(i18n): allow custom interpolation options for i18n Allow for specifying custom interpolation options for i18n. This is useful for custom interpolation patterns, such as `{{` and `}}`. Tests added, and scoped to the i18n module. Closes #4726 * test(i18n): undo describe scoping Unwrapped the tests from the describe blocks, so the diff is cleaner. Prefixed new tests names so they don't collide, also in commented section with begin/end. #4726 * docs(i18n): add docs for interpolation settings #4726 * docs(i18n): fix example #4246
- Loading branch information
Showing
5 changed files
with
328 additions
and
14 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
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,24 +1,30 @@ | ||
import { InterpolateOptions } from '../../shared/types'; | ||
|
||
/** | ||
* Replaces placeholder values in a string with their actual values | ||
*/ | ||
export function interpolate(template: string, values: Record<string, any>): string { | ||
return template.replace(/(\d:)?{([^}]+)}/g, function (_, param, placeholder): string { | ||
export function interpolate(template: string, values: Record<string, any>, options: InterpolateOptions): string { | ||
const { prefix, suffix } = options; | ||
|
||
const regExp = new RegExp(`([0-9]:)?${prefix}([^${suffix}]+)${suffix}`, 'g'); | ||
|
||
return template.replace(regExp, function (_, param, placeholder): string { | ||
if (!param || !values.params) { | ||
return placeholder in values | ||
? values[placeholder] | ||
: values.params && placeholder in values.params | ||
? values.params[placeholder] | ||
: `{${placeholder}}`; | ||
: `${prefix}${placeholder}${suffix}`; | ||
} | ||
|
||
// Handles extended object params format | ||
if (!Array.isArray(values.params)) { | ||
return placeholder in values.params ? values.params[placeholder] : `{${placeholder}}`; | ||
return placeholder in values.params ? values.params[placeholder] : `${prefix}${placeholder}${suffix}`; | ||
} | ||
|
||
// Extended Params exit in the format of `paramIndex:{paramName}` where the index is optional | ||
const paramIndex = Number(param.replace(':', '')); | ||
|
||
return paramIndex in values.params ? values.params[paramIndex] : `${param}{${placeholder}}`; | ||
return paramIndex in values.params ? values.params[paramIndex] : `${param}${prefix}${placeholder}${suffix}`; | ||
}); | ||
} |
Oops, something went wrong.