예제 #1
0
        /// <summary>
        /// Translate the translatable object to culture specific language.
        /// </summary>
        /// <param name="translatableObject">Translatable object to translate</param>
        /// <param name="culture">Culture to translate into</param>
        public static void Translate(this ITranslatable translatableObject, string culture)
        {
            #region validation

            if (translatableObject == null)
            {
                throw new ArgumentNullException(nameof(translatableObject));
            }

            if (string.IsNullOrEmpty(culture))
            {
                throw new ArgumentNullException(nameof(culture));
            }

            #endregion

            // check if translations are avaiable
            if (translatableObject.Translations != null)
            {
                // search for translation with target culture
                Translation translation = translatableObject.GetTranslation(culture);

                // search for fallback translation
                if (translation == null)
                {
                    translation = translatableObject.GetTranslationFallBack(culture);
                }

                if (translation != null)
                {
                    // translate all properties by name
                    Type translationType = translatableObject.GetType();
                    foreach (TranslationText translationText in translation.Texts)
                    {
                        translationType.GetProperty(translationText.Name).SetValue(translatableObject, translationText.Text);
                    }
                }
            }
        }