-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7061 from ever-co/feat/download-translation
Feat/download translation
- Loading branch information
Showing
12 changed files
with
97 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class SupportedLanguage { | ||
// Supported languages | ||
public static get list(): string[] { | ||
return ['ar', 'bg', 'de', 'en', 'es', 'fr', 'he', 'it', 'nl', 'pl', 'pt', 'ru', 'zh']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { argv } from 'yargs'; | ||
import fetch from 'node-fetch'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { env } from '../env'; | ||
import { SupportedLanguage } from './supported-language'; | ||
|
||
export class TranslationUtil { | ||
public readonly desktop: string; | ||
public readonly destination: string; | ||
public readonly i18nUrl: string; | ||
public readonly translations = []; | ||
|
||
constructor() { | ||
this.desktop = String(argv.desktop || ''); | ||
this.destination = path.join('apps', this.desktop, 'src', 'assets', 'i18n'); | ||
this.i18nUrl = env.I18N_FILES_URL; | ||
} | ||
|
||
private validateUrl() { | ||
if (!this.i18nUrl) { | ||
console.warn('WARNING: No translation files url provided'); | ||
return false; | ||
} | ||
try { | ||
const url = new URL(this.i18nUrl); | ||
return !!url; | ||
} catch (error) { | ||
console.error(`ERROR: Url '${this.i18nUrl}' provided is not valid: ` + error); | ||
return false; | ||
} | ||
} | ||
|
||
private async download(language: string): Promise<void> { | ||
const url = `${this.i18nUrl}/${language}.json`; | ||
const response = await fetch(url); | ||
const data = await response.json(); | ||
const fileName = path.basename(url); | ||
fs.writeFileSync(path.join(this.destination, fileName), JSON.stringify(data, null, 4)); | ||
console.log(`✔ File ${fileName} downloaded.`); | ||
} | ||
|
||
private copyAll(): void { | ||
const source = path.join('apps', 'gauzy', 'src', 'assets', 'i18n'); | ||
fs.cpSync(source, this.destination, { recursive: true }); | ||
console.log(`✔ All translations copied from ${source}.`); | ||
} | ||
|
||
public async downloadAll(): Promise<void> { | ||
if (!fs.existsSync(this.destination)) { | ||
console.log(`✔ I18n directory created.`); | ||
fs.mkdirSync(this.destination); | ||
} | ||
if (!this.validateUrl()) { | ||
this.copyAll(); | ||
return; | ||
} | ||
for (const language of SupportedLanguage.list) { | ||
try { | ||
await this.download(language); | ||
} catch (error) { | ||
console.error(`ERROR: Language '${language}' not available: ` + error); | ||
} | ||
} | ||
} | ||
} | ||
|
||
(async () => { | ||
const translationUtil = new TranslationUtil(); | ||
await translationUtil.downloadAll(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters