/// <summary> /// Loads translation key/value pairs from all relevant JSON files within the Asset Manager. /// </summary> /// <param name="logger">The <see cref="ILogger" /> instance used within the sided API.</param> /// <param name="assetManager">The <see cref="IAssetManager" /> instance used within the sided API.</param> /// <param name="languageCode">The language code to use as the default language.</param> public static void LoadLanguage(ILogger logger, IAssetManager assetManager, string languageCode = "en", bool lazyLoad = false) { if (AvailableLanguages.ContainsKey(languageCode)) { AvailableLanguages[languageCode].UseAssetManager(assetManager); AvailableLanguages[languageCode].Load(lazyLoad); return; } var translationService = new TranslationService(languageCode, logger, assetManager); translationService.Load(); AvailableLanguages.Add(languageCode, translationService); }
/// <summary> /// Change the language, this will only do something if the language actually changed. /// All files are reloaded. /// </summary> /// <param name="ietf">The iso code for the language to use</param> /// <param name="cancellationToken">CancellationToken for the loading</param> /// <returns>Task</returns> public async Task ChangeLanguageAsync(string ietf, CancellationToken cancellationToken = default) { if (ietf == CurrentLanguage) { return; } Log.Verbose().WriteLine("Changing language to {0}", ietf); if (AvailableLanguages.ContainsKey(ietf)) { CurrentLanguage = ietf; await ReloadAsync(cancellationToken).ConfigureAwait(false); } else { Log.Warn().WriteLine("Language {0} was not available.", ietf); } }
/// <summary> /// Loads all languages. /// </summary> /// <param name="logger">The <see cref="ILogger" /> instance used within the sided API.</param> /// <param name="assetManager">The <see cref="IAssetManager" /> instance used within the sided API.</param> /// <param name="defaultLanguage">The language code to set as the default/fallback language for the game.</param> public static void Load(ILogger logger, IAssetManager assetManager, string defaultLanguage = "en") { CurrentLocale = defaultLanguage; var languageFile = Path.Combine(GamePaths.AssetsPath, "game", "lang", "languages.json"); var json = JsonObject.FromJson(File.ReadAllText(languageFile)).AsArray(); foreach (var jsonObject in json) { var languageCode = jsonObject["code"].AsString(); LoadLanguage(logger, assetManager, languageCode, languageCode != defaultLanguage); } if (!AvailableLanguages.ContainsKey(defaultLanguage)) { logger.Error("Language '{0}' not found. Will default to english.", defaultLanguage); CurrentLocale = "en"; } }