Skip to content

Commit

Permalink
Merge pull request #26 from imnaiyar/translation
Browse files Browse the repository at this point in the history
feat: Support multiple languages
  • Loading branch information
imnaiyar authored Jun 19, 2024
2 parents 34bf5c5 + 75247fb commit 1f43c3a
Show file tree
Hide file tree
Showing 93 changed files with 13,065 additions and 1,526 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules/
logs/
website/
web/
web/
docs/
jest.config.js
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"curly": ["error", "multi-line", "consistent"],
"dot-location": ["error", "property"],
"handle-callback-err": "off",
"no-unused-vars": "off",
"keyword-spacing": "error",
"max-nested-callbacks": ["error", { "max": 4 }],
"max-statements-per-line": ["error", { "max": 2 }],
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ bun.lockb
coverage/
Dockerfile
docker-compose.debug.yml
.b.env
.b.env
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### Progress
[![Crowdin](https://badges.crowdin.net/skyhelper/localized.svg)](https://crowdin.com/project/skyhelper)

<h1 align="center">
<br>
<a href="https://github.com/imnaiyar/SkyHelper"><img src="https://skyhelper.xyz/assets/img/boticon.png" height="200" alt="SkyHelper"></a>
Expand All @@ -6,7 +9,7 @@
<br>
</h1>

<p align="center">Shards, Next Shards, Seasonal Guides, Timestamp, Sky Times and more...</p>
<p align="center">A Sky COTL Discord Bot</p>
<p align="center"><img src="https://img.shields.io/badge/javascript-%23323330.svg?style=for-the-badge&logo=javascript&logoColor=%23F7DF1E"/> <img src="https://img.shields.io/badge/MongoDB-%234ea94b.svg?style=for-the-badge&logo=mongodb&logoColor=white"/> <img src="https://img.shields.io/github/stars/imnaiyar/SkyHelper"/> <img alt="GitHub release (with filter)" src="https://img.shields.io/github/v/release/imnaiyar/SkyHelper"> <img alt="GitHub License" src="https://img.shields.io/github/license/imnaiyar/SkyHelper">
<img alt="GitHub package.json dependency version (subfolder of monorepo)" src="https://img.shields.io/github/package-json/dependency-version/imnaiyar/SkyHelper/discord.js">
</p>
Expand All @@ -23,11 +26,22 @@
<a href="./documentations/Credits.md">Credits</a>
<a href="https://discord.com/invite/u9zUjWbbQ4">Support Server</a>
<a href="https://discord.com/invite/2rjCRKZsBb">Support Server</a>
</p>

<br>

### Features
- 🌋 Shards
- 🗓 Shards calendar
- 🧮 Seasonal currency calculator
- 🕑 In-game events times and countdowns
- ⏰️ Reminders (Geyser, Grandma, etc...)
- 🟢 Live Updates
- 🈯️ Supports mulitple language ([help us](https://docs.skyhelper.xyz/pages/translating) support more)
- and much more...


## Building the bot

- Clone this repository by running
Expand Down
33 changes: 33 additions & 0 deletions __tests__/getTranslator.test.ts
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("Это я...");
});
});
34 changes: 34 additions & 0 deletions __tests__/useTranslation.test.ts
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);
});
});

3 changes: 3 additions & 0 deletions crowdin.yml
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
2 changes: 1 addition & 1 deletion dashboard/controllers/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Test, TestingModule } from "@nestjs/testing";
import { AppController } from "./app.controller.js";

//Not actually implemented
// Not actually implemented
describe("AppController", () => {
let appController: AppController;

Expand Down
7 changes: 5 additions & 2 deletions dashboard/controllers/guild.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LiveTimes as Times, LiveShard as Shard, Reminders } from "../managers/i
import { ChannelType } from "discord.js";
import getSettings from "../utils/getSettings.js";
import type { Features, GuildInfo } from "../types.js";
import { supportedLang } from "#src/libs/constants/supportedLang";
@Controller("/guilds/:guild")
export class GuildController {
// eslint-disable-next-line
Expand All @@ -25,7 +26,7 @@ export class GuildController {
name: data.name,
icon: data.icon,
prefix: settings?.prefix,
language: "hi-IN",
language: settings?.language?.value ?? "en-US",
announcement_channel: settings?.annoucement_channel ?? undefined,
beta: settings?.beta,
enabledFeatures: actives,
Expand All @@ -40,11 +41,13 @@ export class GuildController {
settings.prefix = body.prefix ?? "";
settings.beta = body.beta || false;
settings.annoucement_channel = body.announcement_channel ?? "";
const language = supportedLang.find((l) => l.value === body.language);
settings.language = language;
await settings.save();
return {
prefix: settings.prefix,
beta: settings.beta,
language: "hi-IN",
language: settings.language?.value ?? "en-US",
announcement_channel: settings.annoucement_channel ?? undefined,
};
}
Expand Down
16 changes: 9 additions & 7 deletions dashboard/controllers/stats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ export class StatsController {
@Get("spirits")
async getSpirits(): Promise<SpiritData[]> {
const spirits = this.bot.spiritsData;
const toReturn = Object.entries(spirits).map(([k, v]) => {
const emoji = v.call || v.emote || v.action || v.stance;
const id = emoji && parseEmoji(emoji.icon)?.id;
const url = id && this.bot.emojis.cache.get(id)?.imageURL();
const t = { name: v.name, value: k, ...(url && { icon: url }) };
return t;
});
const toReturn = Object.entries(spirits)
.filter(([, v]) => v.season)
.map(([k, v]) => {
const emoji = v.call || v.emote || v.action || v.stance;
const id = emoji && parseEmoji(emoji.icon)?.id;
const url = id && this.bot.emojis.cache.get(id)?.imageURL();
const t = { name: v.name, value: k, ...(url && { icon: url }) };
return t;
});
return toReturn;
}
}
32 changes: 32 additions & 0 deletions dashboard/controllers/user.controller.ts
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,
};
}
}
3 changes: 2 additions & 1 deletion dashboard/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { GuildController } from "./controllers/guild.controller.js";
import { AuthMiddleware } from "./middlewares/auth.middleware.js";
import { StatsController } from "./controllers/stats.controller.js";
import { UpdateController } from "./controllers/update.controller.js";
import { UsersController } from "./controllers/user.controller.js";
export async function Dashboard(client: SkyHelper) {
@Module({
imports: [],
controllers: [AppController, GuildController, StatsController, UpdateController],
controllers: [AppController, GuildController, StatsController, UpdateController, UsersController],
providers: [{ provide: "BotClient", useValue: client }],
})
class AppModule implements NestModule {
Expand Down
4 changes: 3 additions & 1 deletion dashboard/managers/LiveShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import moment from "moment";
import getSettings from "../utils/getSettings.js";
import type { TextChannel } from "discord.js";
import type { SkyHelper as BotService } from "#structures";
import { getTranslator } from "#src/i18n";

export class LiveShard {
static async get(client: BotService, guildId: string) {
Expand All @@ -18,7 +19,8 @@ export class LiveShard {
const data = await getSettings(client, guildId);
if (!data) return null;
const now = moment().tz(client.timezone);
const response = buildShardEmbed(now, "Live Shard (updates every 5 min.)", true);
const t = getTranslator(data.language?.value ?? "en-US");
const response = buildShardEmbed(now, t, t("shards-embed.FOOTER"), true);
if (data.autoShard.webhook.id) {
const wb = await client.fetchWebhook(data.autoShard.webhook.id).catch(() => {});
if (wb) {
Expand Down
6 changes: 4 additions & 2 deletions dashboard/managers/LiveTimes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getEventEmbed } from "#handlers";
import { getTimesEmbed } from "#handlers";
import { getTranslator } from "#src/i18n";
import type { SkyHelper as BotService } from "#structures";
import getSettings from "../utils/getSettings.js";
import type { TextChannel, WebhookMessageCreateOptions } from "discord.js";
Expand All @@ -16,7 +17,8 @@ export class LiveTimes {
static async patch(client: BotService, guildId: string, body: any) {
const data = await getSettings(client, guildId);
if (!data) return null;
const embed = await getEventEmbed(client);
const t = getTranslator(data.language?.value ?? "en-US");
const embed = await getTimesEmbed(client, t);
const response: WebhookMessageCreateOptions = { embeds: [embed] };
if (data.autoTimes.webhook.id) {
const wb = await client.fetchWebhook(data.autoTimes.webhook.id).catch(() => {});
Expand Down
4 changes: 4 additions & 0 deletions dashboard/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export type Features = {
};
reminders: ReminderFeature;
};
export interface UserInfo {
language?: string;
}

export interface GuildInfo {
id?: string;
name?: string;
Expand Down
2 changes: 2 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.next
node_modules
21 changes: 21 additions & 0 deletions docs/LICENSE
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.
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#### Docs repo for SkyHelper

https://docs.skyhelper.xyz
21 changes: 21 additions & 0 deletions docs/components/InfoBox.tsx
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 };
18 changes: 18 additions & 0 deletions docs/components/WarningBox.tsx
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 };
6 changes: 6 additions & 0 deletions docs/components/counters.module.css
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;
}
24 changes: 24 additions & 0 deletions docs/components/counters.tsx
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 />;
}
Loading

0 comments on commit 1f43c3a

Please sign in to comment.