Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.

Commit aabd24b

Browse files
committed
add TranslationManager
add Google GSON dependence add ru_ru and en_us
1 parent 50d6fc2 commit aabd24b

7 files changed

Lines changed: 123 additions & 18 deletions

File tree

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies {
3232
implementation "org.telegram:telegrambots-longpolling:${project.telegrambots_api_version}"
3333
implementation "org.telegram:telegrambots-client:${project.telegrambots_api_version}"
3434
implementation "io.github.cdimascio:java-dotenv:${project.java_dotenv_version}"
35+
implementation "com.google.code.gson:gson:${gson_version}"
3536
}
3637

3738
tasks.named('shadowJar') {

gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ version = 0.0.0
55
# Dependencies
66
logback_classic_version = 1.5.21
77
telegrambots_api_version = 9.2.0
8-
java_dotenv_version = 5.2.2
8+
java_dotenv_version = 5.2.2
9+
gson_version = 2.13.2

src/main/java/com/didanko228/tghealthreminder/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.didanko228.tghealthreminder.utils.Config;
44
import com.didanko228.tghealthreminder.utils.Logger;
5+
import com.didanko228.tghealthreminder.utils.TranslationManager;
56
import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication;
67

78
public class Main {
@@ -11,6 +12,7 @@ public class Main {
1112
static void main() {
1213
try (TelegramBotsLongPollingApplication botsApplication = new TelegramBotsLongPollingApplication()) {
1314
botsApplication.registerBot(Config.BOT_TOKEN, new TgHealthReminder(Config.BOT_TOKEN));
15+
TranslationManager.loadTranslations();
1416

1517
Logger.info("Started!");
1618

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.didanko228.tghealthreminder.handlers;
22

33
import com.didanko228.tghealthreminder.utils.Logger;
4+
import com.didanko228.tghealthreminder.utils.TranslationManager;
45
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
56
import org.telegram.telegrambots.meta.api.objects.message.Message;
67
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
@@ -14,24 +15,47 @@ public static void handle(TelegramClient telegramClient, Message msg) {
1415
String text = msg.getText();
1516
long chat_id = msg.getChatId();
1617

17-
SendMessage message = SendMessage
18-
.builder()
19-
.chatId(chat_id)
20-
.text(text)
21-
.replyMarkup(InlineKeyboardMarkup
22-
.builder()
23-
.keyboardRow(new InlineKeyboardRow(InlineKeyboardButton
24-
.builder()
25-
.text("test")
26-
.callbackData("test")
27-
.build()))
28-
.build())
29-
.build();
18+
if (text.equals("ru")) {
19+
SendMessage message = SendMessage
20+
.builder()
21+
.chatId(chat_id)
22+
.text(TranslationManager.translate("ru_ru", "ru_ru"))
23+
.replyMarkup(InlineKeyboardMarkup
24+
.builder()
25+
.keyboardRow(new InlineKeyboardRow(InlineKeyboardButton
26+
.builder()
27+
.text(TranslationManager.translate("ru_ru", "ru_ru"))
28+
.callbackData("test")
29+
.build()))
30+
.build())
31+
.build();
3032

31-
try {
32-
telegramClient.execute(message);
33-
} catch (TelegramApiException e) {
34-
Logger.error("Error sending message", e);
33+
try {
34+
telegramClient.execute(message);
35+
} catch (TelegramApiException e) {
36+
Logger.error("Error sending message", e);
37+
}
38+
}
39+
else if (text.equals("en")) {
40+
SendMessage message = SendMessage
41+
.builder()
42+
.chatId(chat_id)
43+
.text(TranslationManager.translate("en_us", "en_us"))
44+
.replyMarkup(InlineKeyboardMarkup
45+
.builder()
46+
.keyboardRow(new InlineKeyboardRow(InlineKeyboardButton
47+
.builder()
48+
.text(TranslationManager.translate("en_us", "en_us"))
49+
.callbackData("test")
50+
.build()))
51+
.build())
52+
.build();
53+
54+
try {
55+
telegramClient.execute(message);
56+
} catch (TelegramApiException e) {
57+
throw new RuntimeException(e);
58+
}
3559
}
3660
}
3761
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.didanko228.tghealthreminder.utils;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.reflect.TypeToken;
5+
6+
import java.io.*;
7+
import java.net.URL;
8+
import java.net.URLDecoder;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
import java.util.Objects;
13+
import java.util.jar.JarFile;
14+
15+
public class TranslationManager {
16+
private static final Map<String, Map<String, String>> translations = new HashMap<>();
17+
public static final String DEFAULT_LANGUAGE = "ru_ru";
18+
19+
public static void loadTranslations() {
20+
try {
21+
// Получаем URL папки lang
22+
URL url = TranslationManager.class.getClassLoader().getResource("lang");
23+
if (url == null) {
24+
Logger.error("[TranslationManager] lang directory not found!");
25+
return;
26+
}
27+
28+
String protocol = url.getProtocol();
29+
if (protocol.equals("file")) {
30+
// IDE
31+
File folder = new File(url.toURI());
32+
for (File file : Objects.requireNonNull(folder.listFiles(f -> f.getName().endsWith(".json")))) {
33+
loadLocale(file.getName(), new FileInputStream(file));
34+
}
35+
} else if (protocol.equals("jar")) {
36+
// JAR
37+
String jarPath = url.getPath().substring(5, url.getPath().indexOf("!"));
38+
try (JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8))) {
39+
jar.stream()
40+
.filter(entry -> entry.getName().startsWith("lang/") && entry.getName().endsWith(".json"))
41+
.forEach(entry -> {
42+
try (InputStream in = TranslationManager.class.getClassLoader()
43+
.getResourceAsStream(entry.getName())) {
44+
loadLocale(entry.getName().replace("lang/", ""), in);
45+
} catch (IOException e) {
46+
Logger.error("[TranslationManager] Error loading " + entry.getName(), e);
47+
}
48+
});
49+
}
50+
}
51+
} catch (Exception e) {
52+
Logger.error("[TranslationManager] Failed to load translations", e);
53+
}
54+
}
55+
56+
private static void loadLocale(String fileName, InputStream in) throws IOException {
57+
String locale = fileName.replace(".json", "");
58+
Map<String, String> map = new Gson().fromJson(
59+
new BufferedReader(new InputStreamReader(in)),
60+
new TypeToken<Map<String, String>>() {}.getType()
61+
);
62+
translations.put(locale, map);
63+
Logger.info("[TranslationManager] Loaded locale: " + locale);
64+
}
65+
66+
public static String translate(String language, String key, Object... args) {
67+
Map<String, String> localeMap = translations.getOrDefault(language, translations.get(DEFAULT_LANGUAGE));
68+
String template = localeMap.getOrDefault(key, key);
69+
return String.format(template, args);
70+
}
71+
}

src/main/resources/lang/en_us.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"en_us": "\uD83C\uDDFA\uD83C\uDDF8 English"
3+
}

src/main/resources/lang/ru_ru.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ru_ru": "\uD83C\uDDF7\uD83C\uDDFA Русский"
3+
}

0 commit comments

Comments
 (0)