-
Notifications
You must be signed in to change notification settings - Fork 90
Add i18n support for toolbar and table translations #940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AliOsm
wants to merge
4
commits into
basecamp:main
Choose a base branch
from
AliOsm:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,55 @@ | ||
| import en from "./locales/en" | ||
|
|
||
| export default class I18n { | ||
| #locale | ||
| #registry | ||
|
|
||
| constructor(locales = {}, defaultLocale = "en") { | ||
| this.#locale = defaultLocale | ||
| this.#registry = { en, ...locales } | ||
| } | ||
|
|
||
| get locale() { return this.#locale } | ||
| set locale(value) { this.#locale = value } | ||
|
|
||
| registerLocale(name, translations) { | ||
| this.#registry[name] = translations | ||
| } | ||
|
|
||
| t(path, values = {}) { | ||
| let result = resolve(this.#registry[this.#locale], path) | ||
|
|
||
| if (result === undefined && this.#locale !== "en") { | ||
| result = resolve(this.#registry.en, path) | ||
| } | ||
|
|
||
| if (result === undefined) return path | ||
|
|
||
| if (result && typeof result === "object") { | ||
| if (!("count" in values)) return path | ||
| const rule = selectPluralForm(this.#locale, values.count) | ||
| result = result[rule] ?? result.other ?? path | ||
| } | ||
|
|
||
| if (typeof result === "string") { | ||
| return interpolate(result, values) | ||
| } | ||
|
|
||
| return path | ||
| } | ||
| } | ||
|
|
||
| const pluralRulesCache = {} | ||
|
|
||
| function resolve(tree, path) { | ||
| return path.split(".").reduce((node, key) => node?.[key], tree) | ||
| } | ||
|
|
||
| function selectPluralForm(locale, count) { | ||
| pluralRulesCache[locale] ??= new Intl.PluralRules(locale) | ||
| return pluralRulesCache[locale].select(count) | ||
| } | ||
|
|
||
| function interpolate(str, values) { | ||
| return str.replace(/%\{(\w+)\}/g, (_, key) => values[key] ?? `%{${key}}`) | ||
| } |
This file contains hidden or 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 hidden or 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,47 @@ | ||
| export default { | ||
| toolbar: { | ||
| bold: "Bold", | ||
| italic: "Italic", | ||
| format: "Text formatting", | ||
| paragraph: "Normal", | ||
| paragraphTitle: "Paragraph", | ||
| headingLarge: "Large Heading", | ||
| headingLargeTitle: "Large heading", | ||
| headingMedium: "Medium Heading", | ||
| headingMediumTitle: "Medium heading", | ||
| headingSmall: "Small Heading", | ||
| headingSmallTitle: "Small heading", | ||
| strikethrough: "Strikethrough", | ||
| underline: "Underline", | ||
| highlight: "Color highlight", | ||
| removeHighlight: "Remove all coloring", | ||
| link: "Link", | ||
| linkPlaceholder: "Enter a URL\u2026", | ||
| linkSubmit: "Link", | ||
| unlink: "Unlink", | ||
| quote: "Quote", | ||
| code: "Code", | ||
| bulletList: "Bullet list", | ||
| numberedList: "Numbered list", | ||
| table: "Insert a table", | ||
| divider: "Insert a divider", | ||
| undo: "Undo", | ||
| redo: "Redo", | ||
| overflow: "Show more toolbar buttons", | ||
| overflowMenu: "More toolbar buttons", | ||
| uploadImage: "Add images and video", | ||
| uploadFile: "Upload files", | ||
| }, | ||
| table: { | ||
| addBefore: "Add %{childType} before", | ||
| addAfter: "Add %{childType} after", | ||
| add: "Add %{childType}", | ||
| remove: "Remove %{childType}", | ||
| toggleStyle: "Toggle %{childType} style", | ||
| deleteTable: "Delete this table?", | ||
| row: "row", | ||
| column: "column", | ||
| rowCount: { one: "%{count} row", other: "%{count} rows" }, | ||
| columnCount: { one: "%{count} column", other: "%{count} columns" }, | ||
| }, | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
countparameter is passed as the string "_" instead of a number. While the placeholder is replaced with actual numeric counts later by#updateRowColumnCount(), using a non-numeric value forIntl.PluralRules.select()is technically incorrect. The initial placeholder should use a number (e.g., 0) to ensure correct plural form selection from the start.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used
_to preserve the old behavior._will fallback to theotherplural option and produce correct output.