import { AppComponent } from '../app.component'; /* Generator Should Check: -html translate(str, ...) -ts translate(str, ...) */ // @dynamic export class Localization { private static shownErrors = []; private static error = function (error) { if (!Localization.shownErrors[error]) { // show error only one time console.error(error); Localization.shownErrors[error] = true; } } private static putArguments(str: string, args: string[]) { var result = str; var index = result.indexOf("{"); while (index >= 0) { var endIndex = result.indexOf("}", index); var arg = Number(result.substring(index + 1, endIndex)); if (isNaN(arg) || arg < 0 || arg >= args.length) { Localization.error("Localization: invalid argument '" + result.substring(index + 1, endIndex) + "'"); return "---"; } result = result.substring(0, index) + args[arg] + result.substring(endIndex + 1); index = result.indexOf("{"); } return result; } static translate = function (str: string, ...args: string[]): string { if (AppComponent.user.language == AppComponent.client.localizations.default) return Localization.putArguments(str, args); var localizationFile = AppComponent.client.localizations[AppComponent.user.language]; if (!localizationFile) { Localization.error("Localization: file not found! Language: '" + AppComponent.user.language + "'"); return "---"; } var translation = localizationFile[str]; if (!translation) { Localization.error("Localization '" + AppComponent.user.language + "': missing item: '" + str + "'"); translation = "---"; } return Localization.putArguments(translation, args); } // framework default language is english. this translation is needed to convert them to default language, if default is a different language. static translateEn = function (str: string) { if (AppComponent.user.language == "en") return str; var localizationFile = AppComponent.client.localizations[AppComponent.user.language]; if (!localizationFile) { Localization.error("Localization: file not found! Language: '" + AppComponent.user.language + "'"); return "---"; } if (!localizationFile[str]) { Localization.error("Localization '" + AppComponent.user.language + "': missing item: '" + str + "'"); return "---"; } return localizationFile[str]; } }