internal static EnumTranslation <T>[] CreateEnumTranslation <T>(ITranslationSource translationSource) where T : struct, IConvertible
        {
            var type = typeof(T);

            if (!type.IsEnum)
            {
                throw new InvalidOperationException($"The type {type.Name} has to be an enum.");
            }

            if (!type.IsDefined(typeof(TranslatableAttribute), false))
            {
                throw new InvalidOperationException($"The type {type.Name} is no translatable enum! Add the Attribute Translatable to the enum declaration.");
            }

            var values = new List <EnumTranslation <T> >();

            foreach (var value in Enum.GetValues(type).Cast <T>())
            {
                try
                {
                    var context     = ContextAttribute.GetValue(value);
                    var msgid       = TranslationAttribute.GetValue(value);
                    var translation = GetTranslation(msgid, context, translationSource);
                    values.Add(new EnumTranslation <T>(translation, value));
                }
                catch (ArgumentException)
                {
                    throw new InvalidOperationException($"The value {value} in enum {type.Name} does not have the [Translation] attribute. This is required to make it translatable.");
                }
            }

            return(values.ToArray());
        }
 private static string GetTranslation(string msgId, string context, ITranslationSource translationSource = null)
 {
     if (translationSource == null)
     {
         return(msgId);
     }
     return(translationSource.GetTranslation(msgId, context));
 }
        public static void AddSource(ITranslationSource source)
        {
            if (Sources.Contains(source))
            {
                return;
            }

            Sources.Add(source);
        }
Example #4
0
        /// <summary>
        ///     Reverts a temporarily set translation to it's original. If no temporary translation has been set, nothing will be
        ///     reverted.
        /// </summary>
        public void RevertTemporaryTranslation()
        {
            if (_tmpTranslationSource != null)
            {
                TranslationFactory.TranslationSource = _tmpTranslationSource;
            }

            _tmpTranslationSource = null;
        }
        private void SetStringProperty(ITranslatable o, PropertyInfo property, ITranslationSource translationSource)
        {
            var value   = (string)property.GetValue(o, null);
            var context = ContextAttribute.GetValue(property);

            var translated = translationSource.GetTranslation(value, context);

            if (!string.IsNullOrEmpty(translated))
            {
                property.SetValue(o, translated, null);
            }
        }
        private void SetStringArrayProperty(ITranslatable o, PropertyInfo property, IPluralBuilder pluralBuilder,
                                            ITranslationSource translationSource)
        {
            var context = ContextAttribute.GetValue(property);
            var value   = (string[])property.GetValue(o, null);

            if (value.Length != 2)
            {
                throw new InvalidDataException(
                          $"The plural string for key {property.Name} must contain two strings: a singular and a plural form. It contained {value.Length} strings.");
            }

            var translations = translationSource.GetAllTranslations(value[0], context, pluralBuilder);

            if (translations.Length == pluralBuilder.NumberOfPlurals)
            {
                property.SetValue(o, translations, null);
            }
        }
Example #7
0
        private void Translate(ITranslationSource translationSource, ITranslatable targetTranslation, ITranslatable baseTranslation = null)
        {
            if (baseTranslation == null)
            {
                baseTranslation = targetTranslation;
            }

            var type = baseTranslation.GetType();

            var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            var pluralBuilder = translationSource.GetPluralBuilder();

            foreach (var property in properties)
            {
                // TODO: allow to access private setters of inherited fields
                if (!property.CanRead || !property.CanWrite)
                {
                    continue;
                }

                if (property.PropertyType == typeof(string))
                {
                    SetStringProperty(property, translationSource, targetTranslation, baseTranslation);
                }

                if (property.PropertyType == typeof(string[]))
                {
                    SetStringArrayProperty(property, pluralBuilder, translationSource, targetTranslation, baseTranslation);
                }

                if (property.PropertyType.IsArray &&
                    property.PropertyType.GetElementType().Name == typeof(EnumTranslation <>).Name)
                {
                    SetEnumArrayProperty(property, targetTranslation);
                }

                if (property.PropertyType == typeof(IPluralBuilder))
                {
                    SetPluralbuilderProperty(property, pluralBuilder, targetTranslation);
                }
            }
        }
Example #8
0
        private void SetStringArrayProperty(PropertyInfo property, IPluralBuilder pluralBuilder,
                                            ITranslationSource translationSource, ITranslatable targetTranslation, ITranslatable baseTranslation)
        {
            var context       = ContextAttribute.GetValue(property);
            var sourceStrings = (string[])property.GetValue(baseTranslation, null);

            if (sourceStrings.Length != 2)
            {
                throw new InvalidDataException(
                          $"The plural string for key {property.Name} must contain two strings: a singular and a plural form. It contained {sourceStrings.Length} strings.");
            }

            var translations = translationSource.GetAllTranslations(sourceStrings[0], context, pluralBuilder);

            if (translations.Length == pluralBuilder.NumberOfPlurals)
            {
                property.SetValue(targetTranslation, translations, null);
            }
            else
            {
                property.SetValue(targetTranslation, sourceStrings, null);
            }
        }
        private void Translate(ITranslatable o, ITranslationSource translationSource)
        {
            var type = o.GetType();

            var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            var pluralBuilder = translationSource.GetPluralBuilder();

            foreach (var property in properties)
            {
                if (!property.CanRead || !property.CanWrite)
                {
                    continue;
                }

                if (property.PropertyType == typeof(string))
                {
                    SetStringProperty(o, property, translationSource);
                }

                if (property.PropertyType == typeof(string[]))
                {
                    SetStringArrayProperty(o, property, pluralBuilder, translationSource);
                }

                if (property.PropertyType.IsArray &&
                    property.PropertyType.GetElementType().Name == typeof(EnumTranslation <>).Name)
                {
                    SetEnumArrayProperty(o, property);
                }

                if (property.PropertyType == typeof(IPluralBuilder))
                {
                    SetPluralbuilderProperty(o, property, pluralBuilder);
                }
            }
        }
 /// <summary>
 ///     Create a new TranslationFactory with the given ITranslationSource
 /// </summary>
 /// <param name="translationSource">The source that will be used to look up all translations</param>
 public TranslationFactory(ITranslationSource translationSource = null)
 {
     TranslationSource = translationSource;
 }