-
Notifications
You must be signed in to change notification settings - Fork 7
Add localization (i18n) support #15 #46
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
kumaradityaraj
wants to merge
18
commits into
serverlessworkflow:main
Choose a base branch
from
kumaradityaraj:i18nSetup
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 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
625ad89
Implemented i18n basic setup
kumaradityaraj a3ebcb6
Resolved copilots suggestions
kumaradityaraj 6f76d97
Resolved dependency issue
kumaradityaraj 001c00e
Resolved copilot reviews
kumaradityaraj 4cfa4b9
i18n setup provider
kumaradityaraj a7ed3aa
i18n setup provider error resolve
kumaradityaraj bebe436
Changed the approach and implemented i18n similar to kie-tools
kumaradityaraj 89fe327
adding license
kumaradityaraj 6a69e43
Syncing
kumaradityaraj 5b65d94
fixed ts.config
kumaradityaraj 44ff6f3
copilot suggestions
kumaradityaraj 539e69e
Syncing the changes
kumaradityaraj 95ba43b
new implementation
kumaradityaraj 891f88f
tests and readme added
kumaradityaraj f34c637
package.json correction
kumaradityaraj 9862751
package.json correction
kumaradityaraj aef5608
resolving merge conflicts
kumaradityaraj 08f7b31
copilot suggestion
kumaradityaraj 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| <!-- | ||
| Copyright 2021-Present The Serverless Workflow Specification Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
|
|
||
| # i18n Usage Guide | ||
|
|
||
| This guide explains how to use the `@serverlessworkflow/i18n` package inside your project (e.g., in the `DiagramEditor`). | ||
|
|
||
| --- | ||
|
|
||
| ## What this package provides | ||
|
|
||
| - `I18nProvider` → React context provider for translations | ||
| - `useI18n()` → Hook to access translations | ||
| - `detectLocale()` → Automatically detect user language | ||
| - `createI18n()` → Core translation logic (used internally) | ||
|
|
||
| --- | ||
|
|
||
| ## Step 1: Define your dictionaries | ||
|
|
||
| Create a file like: | ||
|
|
||
| ```ts | ||
| // i18n/locales.ts | ||
|
|
||
| export const dictionaries = { | ||
| en: { | ||
| save: "Save", | ||
| }, | ||
| fr: { | ||
| save: "Enregistrer", | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| - Keys (`save`) must be consistent across languages. | ||
|
|
||
| --- | ||
|
|
||
| ## Step 2: Detect or pass locale | ||
|
|
||
| You can either: | ||
|
|
||
| - Pass `locale` manually via props | ||
| - Or auto-detect using `detectLocale` | ||
|
|
||
| Example: | ||
|
|
||
| ```ts | ||
| const supportedLocales = Object.keys(dictionaries); | ||
|
|
||
| const locale = props.locale ?? detectLocale(supportedLocales); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Step 3: Wrap your app with `I18nProvider` | ||
|
|
||
| ```tsx | ||
| import { I18nProvider } from "@serverlessworkflow/i18n"; | ||
| import { dictionaries } from "../i18n/locales"; | ||
|
|
||
| <I18nProvider locale={locale} dictionaries={dictionaries}> | ||
| {/* your app */} | ||
| </I18nProvider>; | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Step 4: Use translations with `useI18n` | ||
|
|
||
| Inside any child component: | ||
|
|
||
| ```tsx | ||
| import { useI18n } from "@serverlessworkflow/i18n"; | ||
|
|
||
| const Content = () => { | ||
| const { t } = useI18n(); | ||
|
|
||
| return <p>{t("save")}</p>; | ||
| }; | ||
| ``` | ||
|
|
||
| - If the key is missing, it will return the key itself: | ||
|
|
||
| ```ts | ||
| t("unknown") → "unknown" | ||
| ``` | ||
|
|
||
| --- |
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,38 @@ | ||
| { | ||
| "name": "@serverlessworkflow/i18n", | ||
kumaradityaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "version": "1.0.0", | ||
kumaradityaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "files": [ | ||
| "dist", | ||
| "src" | ||
| ], | ||
| "type": "module", | ||
| "main": "./dist/index.js", | ||
| "types": "./dist/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "import": "./dist/index.js", | ||
| "types": "./dist/index.d.ts" | ||
| } | ||
| }, | ||
| "scripts": { | ||
kumaradityaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "clean": "rimraf ./dist", | ||
| "build:dev": "pnpm clean && tsc -p tsconfig.json", | ||
| "build:prod": "pnpm run build:dev && pnpm test", | ||
| "test": "vitest run --passWithNoTests" | ||
| }, | ||
| "devDependencies": { | ||
| "@testing-library/jest-dom": "catalog:", | ||
| "@testing-library/react": "catalog:", | ||
| "@types/node": "catalog:", | ||
| "@types/react": "catalog:", | ||
| "@types/react-dom": "catalog:", | ||
| "rimraf": "catalog:", | ||
| "typescript": "catalog:", | ||
| "vite-tsconfig-paths": "catalog:", | ||
| "vitest": "catalog:" | ||
| }, | ||
| "peerDependencies": { | ||
| "react": "^19.0.0", | ||
| "react-dom": "^19.0.0" | ||
| } | ||
| } | ||
kumaradityaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,29 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| export type Dictionary = Record<string, string>; | ||
| export type Dictionaries = Record<string, Dictionary>; | ||
|
|
||
| export function createI18n(dictionaries: Dictionaries, locale: string) { | ||
| function t(key: string): string { | ||
| return dictionaries[locale]?.[key] ?? key; | ||
| } | ||
|
|
||
| return { | ||
| t, | ||
| locale, | ||
| }; | ||
| } |
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,19 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
kumaradityaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| export * from "./react/I18nProvider"; | ||
| export * from "./core/createI18n"; | ||
| export * from "./utils/detectLocale"; | ||
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,46 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import React, { createContext, useContext, useMemo } from "react"; | ||
| import { createI18n, Dictionaries } from "../core/createI18n"; | ||
|
|
||
| type I18nContextType = ReturnType<typeof createI18n>; | ||
|
|
||
| const I18nContext = createContext<I18nContextType | null>(null); | ||
|
|
||
| export const I18nProvider = ({ | ||
| children, | ||
| locale, | ||
| dictionaries, | ||
| }: { | ||
| children: React.ReactNode; | ||
| locale: string; | ||
| dictionaries: Dictionaries; | ||
| }) => { | ||
| const i18n = useMemo(() => { | ||
| return createI18n(dictionaries, locale); | ||
| }, [locale, dictionaries]); | ||
|
|
||
| return <I18nContext.Provider value={i18n}>{children}</I18nContext.Provider>; | ||
| }; | ||
|
|
||
| export const useI18n = () => { | ||
| const ctx = useContext(I18nContext); | ||
| if (!ctx) { | ||
| throw new Error("useI18n must be used inside I18nProvider"); | ||
| } | ||
| return ctx; | ||
| }; |
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,36 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| export function detectLocale(supportedLocales: readonly string[], fallback: string = "en"): string { | ||
| if (typeof navigator === "undefined") { | ||
| return fallback; | ||
| } | ||
| const languages = | ||
| navigator.languages && navigator.languages.length > 0 | ||
| ? navigator.languages | ||
| : [navigator.language]; | ||
|
|
||
| const normalizedSupported = supportedLocales.map((l) => l.toLowerCase().trim()); | ||
| for (const lang of languages) { | ||
| if (!lang) continue; | ||
|
|
||
| const short = lang.split("-")[0]?.toLowerCase().trim(); | ||
| if (short && normalizedSupported.includes(short)) { | ||
| return short; | ||
| } | ||
| } | ||
| return fallback; | ||
| } |
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,40 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { render, screen } from "@testing-library/react"; | ||
| import { describe, it, expect } from "vitest"; | ||
| import { I18nProvider, useI18n } from "../src/react/I18nProvider"; | ||
|
|
||
| const dictionaries = { | ||
| en: { save: "Save" }, | ||
| }; | ||
|
|
||
| const TestComponent = () => { | ||
| const { t } = useI18n(); | ||
| return <span>{t("save")}</span>; | ||
| }; | ||
|
|
||
| describe("I18nProvider", () => { | ||
| it("provides translation", () => { | ||
| render( | ||
| <I18nProvider locale="en" dictionaries={dictionaries}> | ||
| <TestComponent /> | ||
| </I18nProvider>, | ||
| ); | ||
|
|
||
| expect(screen.getByText("Save")).toBeInTheDocument(); | ||
| }); | ||
| }); |
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,35 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { describe, it, expect } from "vitest"; | ||
| import { createI18n } from "../src/core/createI18n"; | ||
|
|
||
| describe("createI18n", () => { | ||
| const dictionaries = { | ||
| en: { save: "Save" }, | ||
| fr: { save: "Enregistrer" }, | ||
| }; | ||
|
|
||
| it("returns correct translation", () => { | ||
| const i18n = createI18n(dictionaries, "en"); | ||
| expect(i18n.t("save")).toBe("Save"); | ||
| }); | ||
|
|
||
| it("returns key if missing", () => { | ||
| const i18n = createI18n(dictionaries, "fr"); | ||
| expect(i18n.t("cancel")).toBe("cancel"); | ||
| }); | ||
| }); |
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,38 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { describe, it, expect, vi } from "vitest"; | ||
| import { detectLocale } from "../src/utils/detectLocale"; | ||
|
|
||
| describe("detectLocale", () => { | ||
| it("detects supported language", () => { | ||
| vi.stubGlobal("navigator", { | ||
| languages: ["fr-FR"], | ||
| }); | ||
|
|
||
| const result = detectLocale(["en", "fr"]); | ||
| expect(result).toBe("fr"); | ||
| }); | ||
|
|
||
| it("falls back if no match", () => { | ||
| vi.stubGlobal("navigator", { | ||
| languages: ["de-DE"], | ||
| }); | ||
|
|
||
| const result = detectLocale(["en", "fr"]); | ||
| expect(result).toBe("en"); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.