-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from imnaiyar/translation
feat: Support multiple languages
- Loading branch information
Showing
93 changed files
with
13,065 additions
and
1,526 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
node_modules/ | ||
logs/ | ||
website/ | ||
web/ | ||
web/ | ||
docs/ | ||
jest.config.js |
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 |
---|---|---|
|
@@ -12,4 +12,4 @@ bun.lockb | |
coverage/ | ||
Dockerfile | ||
docker-compose.debug.yml | ||
.b.env | ||
.b.env |
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,33 @@ | ||
import { getTranslator } from "../src/i18n.js"; | ||
|
||
describe("getTranslator", () => { | ||
it("should return the translated string for a given key and language", () => { | ||
const translator = getTranslator("en-US"); | ||
const translation = translator("common.bot.intro"); | ||
|
||
expect(translation).toMatch("That's me..."); | ||
}); | ||
|
||
it("should throw an error if translation key is not found", () => { | ||
const translator = getTranslator("en-US"); | ||
// @ts-expect-error | ||
expect(() => translator("common.nonexistentKey")).toThrow("Translation key invalid: common.nonexistentKey"); | ||
}); | ||
|
||
it("should return the translated string with interpolation values", () => { | ||
const translator = getTranslator("en-US"); | ||
const translatedString = translator("common.errors.ERROR_ID", { ID: "123456" }); | ||
|
||
expect(translatedString).toBe("Error ID: `123456`"); | ||
}); | ||
|
||
it("should return the translated string for a different language", () => { | ||
const translatorHi = getTranslator("hi"); | ||
const translatorRu = getTranslator("ru"); | ||
const translatedStringHi = translatorHi("common.bot.intro"); | ||
const translatedStringRu = translatorRu("common.bot.intro"); | ||
|
||
expect(translatedStringHi).toMatch("यह में हूं..."); | ||
expect(translatedStringRu).toMatch("Это я..."); | ||
}); | ||
}); |
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,34 @@ | ||
import { supportedLang } from "../src/libs/constants/supportedLang.js"; | ||
import { useTranslations } from "../src/handlers/useTranslation.js"; | ||
const l = supportedLang.map((lang) => lang.value); | ||
type Localizations = Partial<Record<(typeof l)[number], string>>; | ||
|
||
describe("useTranslations", () => { | ||
it("should return the correct translation for a given key", () => { | ||
const translations = useTranslations("common.bot.intro"); | ||
|
||
const translationsTyped: Localizations = translations; | ||
expect(translationsTyped).toBeDefined(); | ||
expect(typeof translationsTyped).toBe("object"); | ||
|
||
for (const key of Object.keys(translationsTyped)) { | ||
expect(l).toContain(key); | ||
expect(typeof translationsTyped[key as (typeof l)[number]]).toBe("string"); | ||
} | ||
}); | ||
|
||
it("should return an empty object if the translation key is not found", () => { | ||
// @ts-expect-error | ||
const translations = useTranslations("common.nonexistentKey"); | ||
|
||
expect(translations).toEqual({}); | ||
expect(Object.keys(translations).length).toBe(0); | ||
}); | ||
|
||
it("should throw an error if the translation value is not a string", () => { | ||
expect(() => { | ||
useTranslations("common.bot"); | ||
}).toThrow(TypeError); | ||
}); | ||
}); | ||
|
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,3 @@ | ||
files: | ||
- source: /locales/en-US.json | ||
translation: /locales/%two_letters_code%.json |
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
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,32 @@ | ||
import { Body, Controller, Get, Inject, Param, Patch } from "@nestjs/common"; | ||
import { SkyHelper as BotService } from "#structures"; | ||
import type { UserInfo } from "../types.js"; | ||
import { supportedLang } from "#src/libs/constants/supportedLang"; | ||
@Controller("/users") | ||
export class UsersController { | ||
// eslint-disable-next-line | ||
constructor(@Inject("BotClient") private readonly bot: BotService) {} | ||
|
||
@Get(":user") | ||
async getUser(@Param("user") userId: string): Promise<UserInfo> { | ||
const user = await this.bot.users.fetch(userId).catch(() => null); | ||
if (!user) return { language: "en-US" }; | ||
const user_settings = await this.bot.database.getUser(user); | ||
return { | ||
language: user_settings.language?.value || "en-US", | ||
}; | ||
} | ||
|
||
@Patch(":user") | ||
async updateUser(@Param("user") userId: string, @Body() data: UserInfo): Promise<UserInfo> { | ||
const user = await this.bot.users.fetch(userId).catch(() => null); | ||
if (!user) return { language: "en-US" }; | ||
const user_settings = await this.bot.database.getUser(user); | ||
const language = supportedLang.find((l) => l.value === data.language); | ||
user_settings.language = language; | ||
await user_settings.save(); | ||
return { | ||
language: data.language, | ||
}; | ||
} | ||
} |
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
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,2 @@ | ||
.next | ||
node_modules |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Shu Ding | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,3 @@ | ||
#### Docs repo for SkyHelper | ||
|
||
https://docs.skyhelper.xyz |
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,21 @@ | ||
import React from "react"; | ||
import { useTheme } from "nextra-theme-docs"; | ||
const InfoBox = ({ children }) => { | ||
const { theme } = useTheme(); | ||
return ( | ||
<div | ||
style={{ | ||
padding: "10px", | ||
borderLeft: "4px solid #0070f3", | ||
backgroundColor: theme === "dark" ? "#001f3f" : "#e0f7fa", | ||
color: theme === "dark" ? "#f0f0f0" : "#000000", | ||
marginBottom: "16px", | ||
borderRadius: "4px", | ||
}} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export { InfoBox }; |
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,18 @@ | ||
import React from "react"; | ||
import { useTheme } from "nextra-theme-docs"; | ||
const WarningBox = ({ children }) => { | ||
const { theme } = useTheme(); | ||
|
||
const boxStyle = { | ||
padding: "10px", | ||
borderLeft: "4px solid #ff9800", | ||
backgroundColor: theme === "dark" ? "#3f1f00" : "#fff3e0", | ||
color: theme === "dark" ? "#f0f0f0" : "#000000", | ||
marginBottom: "16px", | ||
borderRadius: "4px", | ||
}; | ||
|
||
return <div style={boxStyle}>{children}</div>; | ||
}; | ||
|
||
export { WarningBox }; |
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,6 @@ | ||
.counter { | ||
border: 1px solid #ccc; | ||
border-radius: 5px; | ||
padding: 2px 6px; | ||
margin: 12px 0 0; | ||
} |
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,24 @@ | ||
// Example from https://beta.reactjs.org/learn | ||
|
||
import { useState } from "react"; | ||
import styles from "./counters.module.css"; | ||
|
||
function MyButton() { | ||
const [count, setCount] = useState(0); | ||
|
||
function handleClick() { | ||
setCount(count + 1); | ||
} | ||
|
||
return ( | ||
<div> | ||
<button onClick={handleClick} className={styles.counter}> | ||
Clicked {count} times | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export function Counter() { | ||
return <MyButton />; | ||
} |
Oops, something went wrong.