-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(i18n): added support for custom markup
- Loading branch information
Showing
4 changed files
with
100 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { customMarkupPlugin } from "./customMarkupPlugin"; | ||
|
||
describe("customMarkupPlugin", () => { | ||
it("should convert interpolation tags to HTML", () => { | ||
const value = "{{#strong}}Hello{{/strong}} {{#br/}}"; | ||
expect(customMarkupPlugin.process(value, "my_key", {}, null)).toBe( | ||
"<strong>Hello</strong> <br />" | ||
); | ||
}); | ||
|
||
it("should convert interpolation tags to HTML with multiple tags", () => { | ||
const value = "{{#strong}}Hello{{/strong}} {{#br/}} {{#em}}World{{/em}}"; | ||
expect(customMarkupPlugin.process(value, "my_key", {}, null)).toBe( | ||
"<strong>Hello</strong> <br /> <em>World</em>" | ||
); | ||
}); | ||
|
||
it("should not convert interpolation tags to HTML with nested tags", () => { | ||
const value = "{{#strong}}Hello {{#em}}World{{/em}}{{/strong}}"; | ||
expect(customMarkupPlugin.process(value, "my_key", {}, null)).toBe( | ||
"<strong>Hello {{#em}}World{{/em}}</strong>" | ||
); | ||
}); | ||
|
||
it("should not convert interpolation tags to HTML with unclosed tags", () => { | ||
const value = "{{#strong}}Hello"; | ||
expect(customMarkupPlugin.process(value, "my_key", {}, null)).toBe( | ||
"{{#strong}}Hello" | ||
); | ||
}); | ||
|
||
it("should not convert interpolation tags to HTML with different opening an closing tags", () => { | ||
const value = "{{#b}}Hello{{/strong}}"; | ||
expect(customMarkupPlugin.process(value, "my_key", {}, null)).toBe( | ||
"{{#b}}Hello{{/strong}}" | ||
); | ||
}); | ||
}); |
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,20 @@ | ||
import type { PostProcessorModule } from "i18next"; | ||
|
||
const RANGE_REGEX = /{{#(\w+)}}(.*?){{\/\1}}/g; | ||
const SELF_CLOSING_REGEX = /{{#(\w+)\/}}/g; | ||
|
||
/** | ||
* Custom i18next post processor to HTML interpolation tags to HTML. | ||
* | ||
* For example, `{{#strong}}Hello{{/strong}}` will be converted to `<strong>Hello</strong>`, | ||
* and `{{#br/}}` will be converted to `<br />`. | ||
*/ | ||
export const customMarkupPlugin: PostProcessorModule = { | ||
type: "postProcessor", | ||
name: "customMarkup", | ||
process(value: string) { | ||
return value | ||
.replace(RANGE_REGEX, "<$1>$2</$1>") | ||
.replace(SELF_CLOSING_REGEX, "<$1 />"); | ||
}, | ||
}; |
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,17 +1,21 @@ | ||
import i18next from "i18next"; | ||
import { initReactI18next } from "react-i18next"; | ||
import { customMarkupPlugin } from "./customMarkupPlugin"; | ||
|
||
export function initI18next(locale: string) { | ||
i18next.use(initReactI18next).init({ | ||
resources: { | ||
[`${locale}`]: {}, | ||
}, | ||
lng: locale, | ||
lowerCaseLng: true, | ||
interpolation: { | ||
escapeValue: false, | ||
}, | ||
keySeparator: false, | ||
pluralSeparator: ".", | ||
}); | ||
i18next | ||
.use(initReactI18next) | ||
.use(customMarkupPlugin) | ||
.init({ | ||
resources: { | ||
[`${locale}`]: {}, | ||
}, | ||
lng: locale, | ||
lowerCaseLng: true, | ||
interpolation: { | ||
escapeValue: false, | ||
}, | ||
keySeparator: false, | ||
pluralSeparator: ".", | ||
}); | ||
} |