public static List <TranslationDto> GetTranslationDtos(TranslationsTypeEnum translationType) { if (translationType == TranslationsTypeEnum.Base) { return(GetBaseTranslationDtos()); } return(GetPoliticiansTranslationDtos()); }
public static TranslationDto GetTranslationDtoByKey(string key, TranslationsTypeEnum translationsType) { if (translationsType == TranslationsTypeEnum.Base) { return(GetBaseTranslationDtos().FirstOrDefault(t => t.Key == key)); } return(GetPoliticiansTranslationDtos().FirstOrDefault(t => t.Key == key)); }
public static void DeleteTranslation(LanguageEnum language, string key, TranslationsTypeEnum translationType) { var translations = GetTranslationsByLanguageAndType(language, translationType); if (translations.ContainsKey(key)) { translations.Remove(key); SaveTranslations(language, translations, translationType); } }
public static void SaveTranslationDto(TranslationDto translationDto, TranslationsTypeEnum translationType) { var newUkranianTranslation = translationDto.Translations.FirstOrDefault(t => t.Key == LanguageEnum.Ukrainian); var newRussianTranslation = translationDto.Translations.FirstOrDefault(t => t.Key == LanguageEnum.Russian); if (newUkranianTranslation.Equals(default(KeyValuePair <LanguageEnum, string>)) || newRussianTranslation.Equals(default(KeyValuePair <LanguageEnum, string>))) { throw new Exception("There is no translations for one of the languages"); } SaveTranslation(LanguageEnum.Ukrainian, translationDto.Key, newUkranianTranslation.Value, translationType); SaveTranslation(LanguageEnum.Russian, translationDto.Key, newRussianTranslation.Value, translationType); }
public static Dictionary <string, string> GetTranslationsByLanguageAndType(LanguageEnum language, TranslationsTypeEnum translationType) { var translations = new Dictionary <string, string>(); string configKey = string.Empty; switch (language) { case LanguageEnum.Ukrainian: if (translationType == TranslationsTypeEnum.Base) { configKey = translationsUAKey; } else { configKey = politiciansTranslationsUAKey; } break; case LanguageEnum.Russian: if (translationType == TranslationsTypeEnum.Base) { configKey = translationsRUKey; } else { configKey = politiciansTranslationsRUKey; } break; } if (string.IsNullOrEmpty(configKey)) { throw new Exception($"No language resource file found for language: {language.ToString()}."); } return(GetTranslationsByFilePath(ConfigurationManager.AppSettings[configKey])); }
public static void DeleteTranslation(string key, TranslationsTypeEnum translationType) { DeleteTranslation(LanguageEnum.Ukrainian, key, translationType); DeleteTranslation(LanguageEnum.Russian, key, translationType); }
public static void SaveTranslation(LanguageEnum language, string key, string value, TranslationsTypeEnum translationType) { key = key.Trim(); value = value.Trim(); var translations = GetTranslationsByLanguageAndType(language, translationType); if (translations.ContainsKey(key)) { translations[key] = value; } else { translations.Add(key, value); } SaveTranslations(language, translations, translationType); }
public static void SaveTranslations(LanguageEnum language, Dictionary <string, string> translations, TranslationsTypeEnum translationType) { string configKey = string.Empty; switch (language) { case LanguageEnum.Ukrainian: if (translationType == TranslationsTypeEnum.Base) { configKey = translationsUAKey; } else { configKey = politiciansTranslationsUAKey; } break; case LanguageEnum.Russian: if (translationType == TranslationsTypeEnum.Base) { configKey = translationsRUKey; } else { configKey = politiciansTranslationsRUKey; } break; } if (string.IsNullOrEmpty(configKey)) { throw new Exception($"No language resource file found for language: {language.ToString()}."); } var translationsPath = $"{AppDomain.CurrentDomain.BaseDirectory}/{ConfigurationManager.AppSettings[configKey]}"; if (File.Exists(translationsPath)) { var translationsJson = JsonConvert.SerializeObject(translations); File.WriteAllText(translationsPath, translationsJson); } }
public static string GetTranslationByLanguageAndKeyAnyType(LanguageEnum language, string key, TranslationsTypeEnum translationsType) { var translations = GetTranslationDtos(translationsType); if (translations.Any(translation => translation.Key == key)) { var translationDto = translations.FirstOrDefault(t => t.Key == key); var neededTranslation = translationDto.Translations.FirstOrDefault(t => t.Key == language); if (neededTranslation.Equals(default(KeyValuePair <LanguageEnum, string>))) { throw new Exception($"There is no translation with key {key} in language {language}."); } return(neededTranslation.Value); } throw new Exception("Key wasn't found in dictionary."); }