public void GetCultureInfo_StringIsNotValidCulture_ThrowsCultureNotFoundException(bool withBrackets) { //Arrange var culture = withBrackets ? "(llllll)" : "llllll"; //Act //Assert Assert.ThrowsException <CultureNotFoundException>(() => CultureInfoUtil.GetCultureInfo(culture, false)); }
/// <summary> /// Converts string to CultureInfo. /// The String is expected to contain the correct language code in brackets at the end. /// Will always return at least a default value (null). /// </summary> /// <param name="value">The <see cref="string" /> object that should be converted.</param> /// <param name="targetType"> /// The expected <paramref name="targetType" /> is CultureInfo, however receiving a different /// type does not influence what is returned. /// </param> /// <param name="parameter">This converter does not use parameters.</param> /// <param name="culture">This converter does not use <paramref name="culture" />.</param> /// <returns> /// The corresponding <see cref="CultureInfo" /> object or null, if <paramref name="value" /> is not of type /// <see cref="string" /> or an invalid language code. /// </returns> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value is string langString) { return(CultureInfoUtil.GetCultureInfoOrDefault(langString, true)); } return(null); }
public void TestToUCultureInfoAllCultures() { var cultures = CultureInfoUtil.GetAllCultures(); foreach (var culture in cultures) { Assert.IsNotNull(culture.ToUCultureInfo().culture); } }
public void GetCultureInfo_EmptyString_ThrowsCultureNotFoundException(bool withBrackets) { //Arrange var culture = string.Empty; //Act //Assert Assert.ThrowsException <CultureNotFoundException>(() => CultureInfoUtil.GetCultureInfo(culture, false)); }
public void GetCultureInfo_NullString_ThrowsArgumentNullException(bool withBrackets) { //Arrange string culture = null; //Act //Assert Assert.ThrowsException <ArgumentNullException>(() => CultureInfoUtil.GetCultureInfo(culture, false)); }
public void GetCultureInfo_ValidCultureWithBracketsNotMatchingCall_ThrowsCultureNotFoundException() { //Arrange var culture = new CultureInfo("de-DE"); var cultureString = $"({culture.Name})"; //Act //Assert Assert.ThrowsException <CultureNotFoundException>(() => CultureInfoUtil.GetCultureInfo(cultureString, false)); }
public void GetCultureInfoOrDefault_EmptyString_ReturnsNull(bool withBrackets) { //Arrange var culture = string.Empty; //Act var returnCulture = CultureInfoUtil.GetCultureInfoOrDefault(culture, true); //Assert Assert.AreEqual(null, returnCulture); }
public void GetCultureInfoOrDefault_StringIsNotValidCulture_ReturnsNull(bool withBrackets) { //Arrange var culture = withBrackets ? "(llllll)" : "llllll"; //Act var returnCulture = CultureInfoUtil.GetCultureInfoOrDefault(culture, true); //Assert Assert.AreEqual(null, returnCulture); }
public void GetCultureInfoOrDefault_ValidCultureWithBracketsNotMatchingCall_ReturnsNull() { //Arrange var culture = new CultureInfo("de-DE"); var cultureString = $"({culture.Name})"; //Act var returnCulture = CultureInfoUtil.GetCultureInfoOrDefault(cultureString, false); //Assert Assert.AreEqual(null, returnCulture); }
public void GetCultureInfoOrDefault_ValidCultureWithBracketsMatchingCall_ReturnsCorrespondingCultureInfo() { //Arrange var culture = new CultureInfo("de-DE"); var cultureString = $"({culture.Name})"; //Act var returnCulture = CultureInfoUtil.GetCultureInfoOrDefault(cultureString, true); //Assert Assert.AreEqual(culture, returnCulture); }
public void GetCultureInfo_ValidCultureWithoutBracketsNotMatchingCall_ReturnsCorrespondingCultureInfo() { //Arrange var culture = new CultureInfo("de-DE"); var cultureString = culture.Name; //Act var returnCulture = CultureInfoUtil.GetCultureInfo(cultureString, true); //Assert Assert.AreEqual(culture, returnCulture); }
private string GetTranslation(string resourceKey, CultureInfo language, bool exactLanguage) { if (string.IsNullOrEmpty(resourceKey)) { return(null); } //check for changes everytime (changes-dict can update late, incase of ExcelFileProvider). TryAddChangesIntoDictOfDicts(); return(CultureInfoUtil.GetLanguageDictValueOrDefault(_dictOfDicts, language, resourceKey, InputLanguage, exactLanguage)); }
public void TryGetLanguageDict_TargetIsInDictionary_ReturnsCorrespondingTranslation(bool useExact) { //Arrange var dictionary = DictionaryExample; var targetCulture = new CultureInfo("en"); var inputCulture = new CultureInfo("id-ID"); var key = "accept"; var expectedTranslation = DictionaryExample[targetCulture][key]; //Act var returnTranslation = CultureInfoUtil.GetLanguageDictValueOrDefault(dictionary, targetCulture, key, inputCulture, useExact); //Assert Assert.AreEqual(expectedTranslation, returnTranslation); }
public void TryGetLanguageDict_NoMatchesInDictionaryAlAllWithoutExact_ReturnsNull() { //Arrange var dictionary = DictionaryExample; var targetCulture = new CultureInfo("fr"); var inputCulture = new CultureInfo("id-ID"); var key = "accept"; var useExact = false; //Act var returnTranslation = CultureInfoUtil.GetLanguageDictValueOrDefault(dictionary, targetCulture, key, inputCulture, useExact); //Assert Assert.AreEqual(null, returnTranslation); }
public void TryGetLanguageDict_TargetParentIsInDictionaryWithExact_ReturnsNull() { //Arrange var dictionary = DictionaryExample; var targetCulture = new CultureInfo("en-US"); var inputCulture = new CultureInfo("id-ID"); var key = "accept"; var useExact = true; //Act var returnTranslation = CultureInfoUtil.GetLanguageDictValueOrDefault(dictionary, targetCulture, key, inputCulture, useExact); //Assert Assert.AreEqual(null, returnTranslation); }
public void TryGetLanguageDict_NoMatchesInDictionaryWithoutExact_ReturnsInputTranslation() { //Arrange var dictionary = DictionaryExample; var targetCulture = new CultureInfo("fr"); var inputCulture = new CultureInfo("de"); var key = "accept"; var useExact = false; var expectedTranslation = DictionaryExample[inputCulture][key]; //Act var returnTranslation = CultureInfoUtil.GetLanguageDictValueOrDefault(dictionary, targetCulture, key, inputCulture, useExact); //Assert Assert.AreEqual(expectedTranslation, returnTranslation); }
/// <summary> /// Converts IEnumerable<string> to ICollection<CultureInfo>. /// Strings are expected to contain the correct language code in brackets at the end of the string. /// Will always return at least an empty list. /// </summary> /// <param name="value">The IEnumerable<string> object that should be converted.</param> /// <param name="targetType"> /// The expected <paramref name="targetType" /> is IEnumerable<CultureInfo>, however receiving a different /// type does not influence what is returned. /// </param> /// <param name="parameter">This converter does not use parameters.</param> /// <param name="culture">This converter does not use <paramref name="cultur</param> /// <returns> /// The converted sequence. All invalid elements out of <paramref name="value" /> will be excluded. /// </returns> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { ICollection <CultureInfo> languages = new List <CultureInfo>(); if (value is IEnumerable <string> languageStrings) { foreach (var language in languageStrings) { var convertedLanguage = CultureInfoUtil.GetCultureInfoOrDefault(language, true); if (convertedLanguage != null) { languages.Add(convertedLanguage); } } } return(languages); }
public void TryGetLanguageDict_TargetParentIsInDictionaryWithoutExact_ReturnsParentTranslation() { //Arrange var dictionary = DictionaryExample; var targetCulture = new CultureInfo("en-US"); var parentOfTarget = targetCulture.Parent; var inputCulture = new CultureInfo("id-ID"); var key = "accept"; var useExact = false; var expectedTranslation = DictionaryExample[parentOfTarget][key]; //Act var returnTranslation = CultureInfoUtil.GetLanguageDictValueOrDefault(dictionary, targetCulture, key, inputCulture, useExact); //Assert Assert.AreEqual(expectedTranslation, returnTranslation); }
public void TryGetLanguageDict_AnyParameterNull_ThrowsArgumentNullException() { //Arrange var dictionary = DictionaryExample; var targetCulture = new CultureInfo("en-UK"); var inputCulture = new CultureInfo("en-US"); var key = "key"; var useExact = false; //Act //Assert Assert.ThrowsException <ArgumentNullException>(() => CultureInfoUtil.GetLanguageDictValueOrDefault(null, targetCulture, key, inputCulture, useExact)); Assert.ThrowsException <ArgumentNullException>(() => CultureInfoUtil.GetLanguageDictValueOrDefault(dictionary, null, key, inputCulture, useExact)); Assert.ThrowsException <ArgumentNullException>(() => CultureInfoUtil.GetLanguageDictValueOrDefault(dictionary, targetCulture, null, inputCulture, useExact)); Assert.ThrowsException <ArgumentNullException>(() => CultureInfoUtil.GetLanguageDictValueOrDefault(dictionary, targetCulture, key, null, useExact)); }
private TextLocalization GetLiteral(CultureInfo language, string dialogName, string type, string elementName, bool exactLanguage) { var key = CreateGuiDictionaryKey(dialogName, type, elementName); var dictOfDicts = GetDictionaryFromFileProvider(); var result = CultureInfoUtil.GetLanguageDictValueOrDefault(dictOfDicts, language, key, InputLanguage, exactLanguage); if (result == null && !exactLanguage) { _logger.Log(LogLevel.Debug, "Found no translation for dialog '{0}', type '{1}', name '{2}' and language '{3}'.", dialogName, type, elementName, language); } result = EscapedStringConverter.ToNormalString(result); return(new TextLocalization { Language = language, Text = result }); }
public TimeAndWeatherSettingsViewModel(GameSettingsUpdater settingsUpdater) { Func <GameSettings> s = () => settingsUpdater.CurrentSettings; Func <CultureInfo> currentCultureInfo = () => CultureInfoUtil.GetCulture(s().Other.Language); IsTimeSimulated = new GuiComponentDescriptor.Boolean( "simulate_time", updateValue: settingsUpdater.Updater <bool>((settings, value) => { settings.Gameplay.Time.IsTimeSimulated = value; return(settings); }), isChecked: () => s().Gameplay.Time.IsTimeSimulated); CurrentTime = new GuiComponentDescriptor.Range( "current_time", minValue: 0f, maxValue: 23.999f, updateValue: settingsUpdater.Updater <float>((settings, hour) => { var currentDateTime = settings.Gameplay.Time.CurrentDateTime; settings.Gameplay.Time.CurrentDateTime = new DateTime( currentDateTime.Year, currentDateTime.Month, currentDateTime.Day, hour: 0, minute: 0, second: 0, millisecond: 0, kind: currentDateTime.Kind) + TimeSpan.FromHours(hour); return(settings); }), currentValue: () => { var currentDate = s().Gameplay.Time.CurrentDateTime; return(currentDate.Hour + (currentDate.Minute / 60f)); }, updateDisplayValue: (descritor, str) => { var currentDateTime = s().Gameplay.Time.CurrentDateTime; str.Append(currentDateTime.ToString("t", currentCultureInfo())); }, stepSize: 0.16666667f); CurrentDate = new GuiComponentDescriptor.Range( "current_day", minValue: 1f, maxValue: 365f, updateValue: settingsUpdater.Updater <float>((settings, newDayOfYear) => { var currentDateTime = settings.Gameplay.Time.CurrentDateTime; settings.Gameplay.Time.CurrentDateTime = currentDateTime.SetDayOfYear((int)newDayOfYear); return(settings); }), currentValue: () => { var currentDate = s().Gameplay.Time.CurrentDateTime; return(currentDate.DayOfYear); }, updateDisplayValue: (newDayOfYear, str) => { var currentDate = s().Gameplay.Time.CurrentDateTime; str.Append(currentDate.ToString("m", currentCultureInfo())); }); DayLengthInMinutes = new GuiComponentDescriptor.Range( "day_length_in_minutes", minValue: 1f, maxValue: 200f, updateValue: settingsUpdater.Updater <float>((settings, value) => { settings.Gameplay.Time.DayLengthInMinutes = value; return(settings); }), currentValue: () => s().Gameplay.Time.DayLengthInMinutes, updateDisplayValue: (descriptor, str) => { GuiComponentDescriptor.DisplayNumber(s().Gameplay.Time.DayLengthInMinutes, str, decimalPlaces: 0); }); IsNightSkipped = new GuiComponentDescriptor.Boolean( "skip_night", updateValue: settingsUpdater.Updater <bool>((settings, value) => { settings.Gameplay.Time.IsNightSkipped = value; return(settings); }), isChecked: () => s().Gameplay.Time.IsNightSkipped); IsWeatherSimulated = new GuiComponentDescriptor.Boolean( "simulate_weather", updateValue: settingsUpdater.Updater <bool>((settings, value) => { settings.Gameplay.Weather.IsWeatherSimulated = value; return(settings); }), isChecked: () => s().Gameplay.Weather.IsWeatherSimulated); SnowAltitude = new GuiComponentDescriptor.Range( "snow_altitude", minValue: 2000f, maxValue: 3300, updateValue: settingsUpdater.Updater <float>((settings, value) => { settings.Gameplay.Weather.SnowAltitude = value; return(settings); }), currentValue: () => s().Gameplay.Weather.SnowAltitude, updateDisplayValue: (descriptor, str) => { descriptor.IsVisible = !s().Gameplay.Weather.IsWeatherSimulated; GuiComponentDescriptor.DisplayNumber(s().Gameplay.Weather.SnowAltitude, str, decimalPlaces: 0, postFix: "m"); }, stepSize: 20f); SnowfallIntensity = new GuiComponentDescriptor.Range( "snowfall_intensity", minValue: 0f, maxValue: 1f, updateValue: settingsUpdater.Updater <float>((settings, value) => { settings.Gameplay.Weather.SnowfallIntensity = value; return(settings); }), currentValue: () => s().Gameplay.Weather.SnowfallIntensity, updateDisplayValue: (descriptor, str) => { descriptor.IsVisible = !s().Gameplay.Weather.IsWeatherSimulated; GuiComponentDescriptor.DisplayPercentage(s().Gameplay.Weather.SnowfallIntensity, str); }, stepSize: 0.02f); FogIntensity = new GuiComponentDescriptor.Range( "fog_intensity", minValue: 0f, maxValue: 1f, updateValue: settingsUpdater.Updater <float>((settings, value) => { settings.Gameplay.Weather.FogIntensity = value; return(settings); }), currentValue: () => s().Gameplay.Weather.FogIntensity, updateDisplayValue: (descriptor, str) => { descriptor.IsVisible = !s().Gameplay.Weather.IsWeatherSimulated; GuiComponentDescriptor.DisplayPercentage(s().Gameplay.Weather.FogIntensity, str); }, stepSize: 0.02f); DaysPerSeason = new GuiComponentDescriptor.Range( "days_per_season", minValue: 1f, maxValue: 178f, updateValue: settingsUpdater.Updater <float>((settings, value) => { settings.Gameplay.Weather.DaysPerSeason = value; return(settings); }), currentValue: () => s().Gameplay.Weather.DaysPerSeason, updateDisplayValue: (descriptor, str) => { descriptor.IsVisible = s().Gameplay.Weather.IsWeatherSimulated; GuiComponentDescriptor.DisplayNumber(s().Gameplay.Weather.DaysPerSeason, str, decimalPlaces: 0); }); }