-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(chrome-extension): add i18n foundation #2009
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
Sunnnnch
wants to merge
2
commits into
CapSoftware:main
Choose a base branch
from
Sunnnnch:codex/chrome-extension-i18n
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
2 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,17 @@ | ||
| { | ||
| "actionDefaultTitle": { | ||
| "message": "Record your screen with Cap" | ||
| }, | ||
| "extensionDescription": { | ||
| "message": "Free, open source screen recorder. Capture your screen, tab, camera & mic in Chrome and share a video link the moment you stop." | ||
| }, | ||
| "extensionName": { | ||
| "message": "Cap - Screen Recorder & Screen Capture" | ||
| }, | ||
| "extensionShortName": { | ||
| "message": "Cap" | ||
| }, | ||
| "offscreenJustification": { | ||
| "message": "Record and upload Cap videos from an extension page." | ||
| } | ||
| } |
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,17 @@ | ||
| { | ||
| "actionDefaultTitle": { | ||
| "message": "使用 Cap 录制屏幕" | ||
| }, | ||
| "extensionDescription": { | ||
| "message": "免费开源的屏幕录制工具。在 Chrome 中录制屏幕、标签页、摄像头和麦克风,停止录制后即可分享视频链接。" | ||
| }, | ||
| "extensionName": { | ||
| "message": "Cap - 屏幕录制与截图" | ||
| }, | ||
| "extensionShortName": { | ||
| "message": "Cap" | ||
| }, | ||
| "offscreenJustification": { | ||
| "message": "从扩展页面录制并上传 Cap 视频。" | ||
| } | ||
| } |
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
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
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,71 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import englishMessages from "../../public/_locales/en/messages.json"; | ||
| import chineseMessages from "../../public/_locales/zh_CN/messages.json"; | ||
| import { type MessageKey, msg } from "./i18n"; | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| describe("extension messages", () => { | ||
| it("keeps locale catalogs aligned", () => { | ||
| expect(Object.keys(chineseMessages).sort()).toEqual( | ||
| Object.keys(englishMessages).sort(), | ||
| ); | ||
| }); | ||
|
|
||
| it("uses the browser locale when a translation is available", () => { | ||
| const getMessage = vi.fn(() => "使用 Cap 录制屏幕"); | ||
| vi.stubGlobal("chrome", { i18n: { getMessage } }); | ||
|
|
||
| expect(msg("actionDefaultTitle")).toBe("使用 Cap 录制屏幕"); | ||
| expect(getMessage).toHaveBeenCalledWith("actionDefaultTitle", undefined); | ||
| }); | ||
|
|
||
| it("falls back to English outside an extension context", () => { | ||
| vi.stubGlobal("chrome", undefined); | ||
|
|
||
| expect(msg("actionDefaultTitle")).toBe("Record your screen with Cap"); | ||
| }); | ||
|
|
||
| it("falls back to English when Chrome i18n is unavailable", () => { | ||
| vi.stubGlobal("chrome", {}); | ||
|
|
||
| expect(msg("actionDefaultTitle")).toBe("Record your screen with Cap"); | ||
| }); | ||
|
|
||
| it("falls back to English when Chrome cannot resolve a message", () => { | ||
| vi.stubGlobal("chrome", { | ||
| i18n: { getMessage: vi.fn(() => "") }, | ||
| }); | ||
|
|
||
| expect(msg("offscreenJustification")).toBe( | ||
| "Record and upload Cap videos from an extension page.", | ||
| ); | ||
| }); | ||
|
|
||
| it("applies substitutions to the English fallback", () => { | ||
| const originalMessage = englishMessages.actionDefaultTitle.message; | ||
| vi.stubGlobal("chrome", undefined); | ||
|
|
||
| try { | ||
| englishMessages.actionDefaultTitle.message = "Record $1"; | ||
| expect(msg("actionDefaultTitle", "your screen")).toBe( | ||
| "Record your screen", | ||
| ); | ||
|
|
||
| englishMessages.actionDefaultTitle.message = "Record $1 with $2"; | ||
| expect(msg("actionDefaultTitle", ["your screen", "Cap"])).toBe( | ||
| "Record your screen with Cap", | ||
| ); | ||
| } finally { | ||
| englishMessages.actionDefaultTitle.message = originalMessage; | ||
| } | ||
| }); | ||
|
|
||
| it("returns the key when the English fallback is missing", () => { | ||
| vi.stubGlobal("chrome", undefined); | ||
|
|
||
| expect(msg("missingMessage" as MessageKey)).toBe("missingMessage"); | ||
| }); | ||
| }); |
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,22 @@ | ||
| import englishMessages from "../../public/_locales/en/messages.json"; | ||
|
|
||
| export type MessageKey = keyof typeof englishMessages & string; | ||
|
|
||
| export const msg = (key: MessageKey, substitutions?: string | string[]) => { | ||
| const localizedMessage = | ||
| typeof chrome === "undefined" || | ||
| typeof chrome.i18n?.getMessage !== "function" | ||
| ? "" | ||
| : chrome.i18n.getMessage(key, substitutions); | ||
|
|
||
| if (localizedMessage) return localizedMessage; | ||
|
|
||
| const fallback = englishMessages[key]?.message || key; | ||
| if (!substitutions) return fallback; | ||
|
|
||
| const values = Array.isArray(substitutions) ? substitutions : [substitutions]; | ||
| return fallback.replace( | ||
| /\$(\d+)/g, | ||
| (_match, index: string) => values[Number(index) - 1] ?? "", | ||
| ); | ||
| }; | ||
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.
window.chromecan exist outside an extension context (but withouti18n), sotypeof chrome === "undefined"alone can still blow up here. I’d guardi18n.getMessageas well, and maybe add a test for that case.