Skip to content

Commit

Permalink
i18n rewrite (#99)
Browse files Browse the repository at this point in the history
* add en-US.json

* add file with notes on changes needed

* add dropdown strings

* add new strings

* remove antistar references

* add last couple of strings

* add i18next

* bump d.js, implement i18next

* slight string changes

* fix string

* fix unnecessary escaping
  • Loading branch information
GamingGeek authored Jul 4, 2021
1 parent 8a999c8 commit 5eefd8f
Show file tree
Hide file tree
Showing 132 changed files with 2,404 additions and 4,292 deletions.
7 changes: 7 additions & 0 deletions languages/en-GB.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"LANGUAGE_COMMAND_HELLO_USER": "Hello! You have successfully set Fire's language to English (GB) :D",
"LANGUAGE_COMMAND_HELLO_GUILD": "Hello! You have successfully set Fire's language in this guild to English (GB). Want to set it just for you? Run the command in DMs",
"COLOR_COMMAND_DESCRIPTION": "Get information about a colour",
"COLOR_ARGUMENT_INVALID": "That does not seem to be a valid colour, maybe try {{random}}",
"COLOR_HEADING": "Information about the colour **{{color}}**"
}
1,072 changes: 1,072 additions & 0 deletions languages/en-US.json

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions lib/Fire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ import { Util } from "./util/clientutil";
import * as Sentry from "@sentry/node";
import { Message } from "./ws/Message";
import { Manager } from "./Manager";
import * as i18next from "i18next";
import * as moment from "moment";

// this shit has some weird import fuckery, this is the only way I can use it
const i18n = (i18next as unknown) as typeof i18next.default;

type ButtonHandler = (button: ComponentMessage) => Promise<any> | any;

import "./extensions";
Expand All @@ -86,6 +90,9 @@ export class Fire extends AkairoClient {
started: boolean;
restPing: number;

// i18n
i18n: typeof i18next.default;

// Sharding
manager: Manager;

Expand Down Expand Up @@ -121,6 +128,8 @@ export class Fire extends AkairoClient {
constructor(manager: Manager, sentry?: typeof Sentry) {
super({ ...config.akairo, ...config.discord });

this.i18n = i18n;

// @ts-ignore
this.rest = new RESTManager(this);
this.useCanary = true; // use canary api by default
Expand Down Expand Up @@ -336,6 +345,16 @@ export class Fire extends AkairoClient {
: "./src/languages/",
});
this.languages.loadAll();
i18n
.init({
fallbackLng: "en-US",
fallbackNS: "fire",
resources: {},
lng: "en-US",
})
.then(() => {
this.languages.modules.forEach((language: Language) => language.init());
});

this.modules = new ModuleHandler(this, {
directory: __dirname.includes("/dist/")
Expand Down
41 changes: 32 additions & 9 deletions lib/extensions/componentmessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import {
DMChannel,
Webhook,
} from "discord.js";
import { Language, LanguageKeys } from "../util/language";
import { FireTextChannel } from "./textchannel";
import { APIMessage } from "discord-api-types";
import { TOptions, StringMap } from "i18next";
import { constants } from "../util/constants";
import { Language } from "../util/language";
import { FireMember } from "./guildmember";
import { FireMessage } from "./message";
import { FireGuild } from "./guild";
Expand Down Expand Up @@ -154,16 +155,16 @@ export class ComponentMessage {
return this.snowflake.timestamp;
}

send(key: string = "", ...args: any[]) {
send(key?: LanguageKeys, args?: TOptions<StringMap>) {
return this.channel.send(
{ content: this.language.get(key, ...args) },
{ content: this.language.get(key, args) },
this.flags
);
}

success(
key: string = "",
...args: any[]
key?: LanguageKeys,
args?: TOptions<StringMap>
): Promise<ComponentMessage | MessageReaction | void> {
if (!key) {
if (this.sourceMessage instanceof FireMessage)
Expand All @@ -178,14 +179,36 @@ export class ComponentMessage {
});
}
return this.channel.send(
`${emojis.success} ${this.language.get(key, ...args)}`,
`${emojis.success} ${this.language.get(key, args)}`,
typeof this.flags == "number" ? this.flags : 64
);
}

warn(
key?: LanguageKeys,
args?: TOptions<StringMap>
): Promise<ComponentMessage | MessageReaction | void> {
if (!key) {
if (this.sourceMessage instanceof FireMessage)
return this.sourceMessage.react(reactions.warning).catch(() => {});
else
return this.getRealMessage().then((message) => {
if (!message || !(message instanceof FireMessage))
return this.warn("SLASH_COMMAND_HANDLE_FAIL");
message.react(reactions.warning).catch(() => {
return this.warn("SLASH_COMMAND_HANDLE_FAIL");
});
});
}
return this.channel.send(
`${emojis.warning} ${this.language.get(key, args)}`,
typeof this.flags == "number" ? this.flags : 64
);
}

error(
key: string = "",
...args: any[]
key?: LanguageKeys,
args?: TOptions<StringMap>
): Promise<ComponentMessage | MessageReaction | void> {
if (!key) {
if (this.sourceMessage instanceof FireMessage)
Expand All @@ -200,7 +223,7 @@ export class ComponentMessage {
});
}
return this.channel.send(
`${emojis.error} ${this.language.get(key, ...args)}`,
`${emojis.slashError} ${this.language.get(key, args)}`,
typeof this.flags == "number" ? this.flags : 64
);
}
Expand Down
Loading

0 comments on commit 5eefd8f

Please sign in to comment.