/// <summary> /// Locates the translation in internal (cache) list and returns. /// If not found will return NULL (to ease check) /// </summary> /// <param name="propertyName">Name of the property.</param> /// <param name="language">Value of the SupportedLanguage.</param> /// <param name="textType">Value of the TranslatedTextType</param> /// <returns>Found translation string or NULL if not found</returns> private string LocateTranslation(string propertyName, SupportedLanguage language, TranslatedTextType textType) { // FOR: because it is fastest locator http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html for (int i = 0; i < this.modelElementTranslations.Count; i++) { if (this.modelElementTranslations[i].PropertyName == propertyName && this.modelElementTranslations[i].Language == language && this.modelElementTranslations[i].TranslationType == textType) { return this.modelElementTranslations[i].TranslatedText; } } // not found if came here return null; }
/// <summary> /// Gets the translation of required property in CurrentUiCulture language for requested text type. /// Note: Returns empty string if translation is not found both in requested language and in fallback (default) language /// </summary> /// <param name="propertyName">Name of the property (in view model, which needs translation).</param> /// <param name="textType">Type of the text in web element (out of "Propmpt", "HelpText" and "Placeholder/Default value" etc.).</param> /// <returns>Translation (if found) or empty string if not found.</returns> public string GetTranslation(string propertyName, TranslatedTextType textType) { SupportedLanguage currentLanguage = Globalizer.CurrentUICultureLanguage.Value; return this.ProductionBehavior(propertyName, currentLanguage, textType); }
/// <summary>NOTE: /// ====== THIS METHOD MUST BE USED ONLY FOR TESTING PURPOSES! ====== /// /// Gets the translation of required property in CurrentUiCulture language for requested text type. /// and this Testing version will return "R:PropertyName" for missing translations (not desirable behavior in production) /// Use only within controlled compile time blocks (#if !PROD) /// Secondly - we can have both test-time and runtime behavioral unit-tests /// </summary> /// <param name="propertyName">Name of the property (in view model, which needs translation).</param> /// <param name="textType">Type of the text in web element (out of "Propmpt", "HelpText" and "Placeholder/Default value" etc.).</param> /// <param name="enumPropertyName">Name of the property in enum</param> /// <returns>Translation (if found) or "R:PropertyName" for missing translations</returns> public string GetTestableTranslation(string propertyName, TranslatedTextType textType, string enumPropertyName = null) { SupportedLanguage currentLanguage = Globalizer.CurrentUICultureLanguage.Value; return this.TestingBehavior(propertyName, currentLanguage, textType, enumPropertyName); }
/// <summary> /// Testings behavior as follows: /// 1) If found in requested language - returns it as-is /// 2) Checks fallback/default language - if found - prepends original language code to it and returns, like "fi:This is fallback translation" /// 3) Translation does not exist at all - returns property name prepended with R, like "R:PropertyName" /// </summary> /// <param name="propertyName">Name of the property (in view model, which needs translation).</param> /// <param name="language">Value of the language in SupportedLanguages Enum.</param> /// <param name="textType">Value of the text in web element (out of "Propmpt", "HelpText" and "Placeholder/Default value" etc.) index in defined ENUM.</param> /// <param name="enumPropertyName">Name of the property in enum</param> /// <returns>Translated Text</returns> private string TestingBehavior(string propertyName, SupportedLanguage language, TranslatedTextType textType, string enumPropertyName = null) { string translation = this.LocateTranslation(propertyName, language, textType); if (!string.IsNullOrEmpty(translation)) { return translation; } // if not found in current language and it is not default already - try default language ("0" value in Supported Languages list) if (translation == null && language != SupportedLanguage.English) { translation = this.LocateTranslation(propertyName, SupportedLanguage.English, textType); if (translation == null && textType == TranslatedTextType.Label) { return string.Concat("R:", propertyName); } if (textType == TranslatedTextType.Label) { var initialLanguageName = Globalizer.CurrentUICultureLanguage.Key; return string.Concat(initialLanguageName, ":", translation); } } if (textType == TranslatedTextType.Label || textType == TranslatedTextType.EnumText) { return string.Concat("R:", propertyName); } return string.Empty; }
/// <summary> /// Production behavior is as follows: /// 1) Locate CurrentCulture Language translation, if found - return it /// 2) If not found - try default language (0 = English), if this found - return it /// 3) If not found at all (or list is empty) - return empty string /// </summary> /// <param name="propertyName">Name of the property (in view model, which needs translation).</param> /// <param name="language">Value of the language in SupportedLanguages Enum.</param> /// <param name="textType">Type of the text in web element (out of "Propmpt", "HelpText" and "Placeholder/Default value" etc.) index in defined ENUM.</param> private string ProductionBehavior(string propertyName, SupportedLanguage language, TranslatedTextType textType) { string translation = this.LocateTranslation(propertyName, language, textType); // if not found in current language and it is not default already - try default language ("0" value in Globalizer implemented Language list) if (translation == null && language != Globalizer.ImplementedCultures[0].Value) { translation = this.LocateTranslation(propertyName, Globalizer.ImplementedCultures[0].Value, textType); } return translation ?? string.Empty; }
private void CreateWebTrans(TextTransLang textTransLang, List<WebElementTranslation> WebTransList, TranslatedTextType translatedTextType) { if (!string.IsNullOrEmpty(textTransLang.EngText)) { WebTransList.Add(new WebElementTranslation { TranslationType = translatedTextType, Language = SupportedLanguage.English, TranslatedText = textTransLang.EngText }); // Console.WriteLine("Add: {0} {1}", SupportedLanguage.English, translatedTextType); } if (!string.IsNullOrEmpty(textTransLang.FinText)) { WebTransList.Add(new WebElementTranslation { TranslationType = translatedTextType, Language = SupportedLanguage.Finnish, TranslatedText = textTransLang.FinText }); // Console.WriteLine("Add: {0} {1}", SupportedLanguage.Finnish, translatedTextType); } if (!string.IsNullOrEmpty(textTransLang.SweText)) { WebTransList.Add(new WebElementTranslation { TranslationType = translatedTextType, Language = SupportedLanguage.Swedish, TranslatedText = textTransLang.SweText }); // Console.WriteLine("Add: {0} {1}", SupportedLanguage.Swedish, translatedTextType); } }
/// <summary> /// Method that decides whether WebElement has specified type/langu /// </summary> /// <param name="elementToUpdate">Page translation and either updates/creates/deletes it depending on actual translation string</param> /// <param name="model">Model for translation updating</param> /// <param name="textType">Type of the text</param> private void AddOrUpdateWebObjectTranslation(WebElement elementToUpdate, Web.WebElementModel model, TranslatedTextType textType) { string valueToUpdate = string.Empty; switch (textType) { case TranslatedTextType.Label: valueToUpdate = model.PropertyLabel; break; case TranslatedTextType.SubLabel: valueToUpdate = model.PropertySubLabel; break; case TranslatedTextType.ControlText: valueToUpdate = model.PropertyHint; break; case TranslatedTextType.HelpText: valueToUpdate = model.PropertyHelp; break; case TranslatedTextType.EnumText: valueToUpdate = model.PropertyEnum; break; } var dbTranslation = elementToUpdate.WebElementTranslations.SingleOrDefault( wt => wt.TranslationType == textType && wt.Language == model.LanguageToSave.ToDbObject()); if (!string.IsNullOrEmpty(valueToUpdate)) { if (dbTranslation == null) { // add element elementToUpdate.WebElementTranslations.Add( new WebElementTranslation { Language = model.LanguageToSave.ToDbObject(), TranslatedText = valueToUpdate, TranslationType = textType }); } else { // update element dbTranslation.TranslatedText = valueToUpdate; } } else { if (dbTranslation != null) { // remove element elementToUpdate.WebElementTranslations.Remove(dbTranslation); } } }