feat: i18n support for backend

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