feat: i18n support for backend

This commit is contained in:
cihatata
2025-01-28 00:32:00 +03:00
committed by cihat
parent 10713428b9
commit e18f1d9b85
+3 -17
View File
@@ -9,18 +9,16 @@ class TranslationService {
constructor(logger) {
this.logger = logger;
this.translations = {};
this.apiToken = "ddf8d5fdbe1baa12bb3b5519b639d00a";
this.projectId = "757606";
this.apiToken = process.env.POEDITOR_API_TOKEN;
this.projectId = process.env.POEDITOR_PROJECT_ID;
this.baseUrl = 'https://api.poeditor.com/v2';
this.localesDir = path.join(process.cwd(), 'locales');
}
async initialize() {
try {
// Önce dosyalardan okumayı dene
const loadedFromFiles = await this.loadFromFiles();
// Eğer dosyalardan yüklenemezse veya dosyalar yoksa POEditor'dan çek
if (!loadedFromFiles) {
await this.loadTranslations();
}
@@ -36,19 +34,16 @@ class TranslationService {
async loadFromFiles() {
try {
// locales klasörü yoksa false dön
if (!fs.existsSync(this.localesDir)) {
return false;
}
// Klasördeki tüm .json dosyalarını oku
const files = fs.readdirSync(this.localesDir).filter(file => file.endsWith('.json'));
if (files.length === 0) {
return false;
}
// Her dosyayı oku ve translations objesine ekle
for (const file of files) {
const language = file.replace('.json', '');
const filePath = path.join(this.localesDir, file);
@@ -76,16 +71,13 @@ class TranslationService {
async loadTranslations() {
try {
// Önce mevcut dilleri al
const languages = await this.getLanguages();
// Her dil için çevirileri indir
for (const language of languages) {
const translations = await this.exportTranslations(language);
this.translations[language] = translations;
}
// Çevirileri dosyaya kaydet
await this.saveTranslations();
} catch (error) {
this.logger.error({
@@ -117,7 +109,7 @@ class TranslationService {
method: 'getLanguages',
stack: error.stack
});
return ['en']; // Varsayılan olarak İngilizce
return ['en'];
}
}
@@ -129,17 +121,14 @@ class TranslationService {
params.append('language', language);
params.append('type', 'key_value_json');
// Export isteği
const exportResponse = await axios.post(`${this.baseUrl}/projects/export`, params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
// İndirme URL'sini al
const { url } = exportResponse.data.result;
// Çevirileri indir
const translationsResponse = await axios.get(url);
return translationsResponse.data;
} catch (error) {
@@ -155,12 +144,10 @@ class TranslationService {
async saveTranslations() {
try {
// locales klasörü yoksa oluştur
if (!fs.existsSync(this.localesDir)) {
fs.mkdirSync(this.localesDir);
}
// Her dil için JSON dosyası oluştur
for (const [language, translations] of Object.entries(this.translations)) {
const filePath = path.join(this.localesDir, `${language}.json`);
fs.writeFileSync(filePath, JSON.stringify(translations, null, 2));
@@ -182,7 +169,6 @@ class TranslationService {
}
getTranslation(key, language = 'en') {
// Convert key from AUTH_INCORRECT_PASSWORD format to authIncorrectPassword format
const formattedKeyText = formattedKey(key);
try {
return this.translations[language]?.[formattedKeyText] || this.translations['en']?.[formattedKeyText] || formattedKeyText;