private static void AddKeysFrom(HashSet<string> permissions, ILocalTextRegistry registry, Type type, string languageID)
        {
            var thisKeys = new List<string>();

            foreach (var member in type.GetFields(BindingFlags.Static | BindingFlags.DeclaredOnly |
                BindingFlags.Public | BindingFlags.NonPublic))
            {
                if (member.FieldType != typeof(String))
                    continue;

                var key = member.GetValue(null) as string;
                if (key == null)
                    continue;

                DescriptionAttribute descr;

                if (key.EndsWith(":"))
                {
                    if (registry != null)
                    {
                        descr = member.GetCustomAttribute<DescriptionAttribute>();
                        registry.Add(languageID, "Permission." + key, descr != null ? descr.Description : member.Name);
                    }

                    continue;
                }

                thisKeys.Add(key);

                if (permissions != null)
                    permissions.Add(key);

                if (registry != null)
                {
                    descr = member.GetCustomAttribute<DescriptionAttribute>();
                    registry.Add(languageID, "Permission." + key, descr != null ? descr.Description : member.Name);
                }
            }

            if (registry != null && thisKeys.Count > 0)
            {
                var displayName = type.GetCustomAttribute<DisplayNameAttribute>();

                var lastColonIndex = thisKeys[0].LastIndexOf(":");
                if (displayName != null &&
                    lastColonIndex > 0 &&
                    lastColonIndex < thisKeys[0].Length - 1 &&
                    thisKeys.TrueForAll(x => x.LastIndexOf(":") == lastColonIndex))
                {
                    registry.Add(languageID, "Permission." + thisKeys[0].Substring(0, lastColonIndex + 1), displayName.DisplayName);
                }
            }

            foreach (var nested in type.GetNestedTypes(BindingFlags.Public | BindingFlags.DeclaredOnly))
                AddKeysFrom(permissions, registry, nested, languageID);
        }
        /// <summary>
        /// Adds translation from a hierarchical local text dictionary parsed from JSON file.
        /// </summary>
        /// <param name="nested">Object parsed from local text JSON string</param>
        /// <param name="prefix">Prefix to prepend before local text keys</param>
        /// <param name="languageID">Language ID</param>
        /// <param name="registry">Registry</param>
        public static void AddFromNestedDictionary(IDictionary<string, JToken> nested, string prefix, string languageID, ILocalTextRegistry registry = null)
        {
            if (nested == null)
                throw new ArgumentNullException("nested");

            var target = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            ProcessNestedDictionary(nested, prefix, target);

            registry = registry ?? Dependency.Resolve<ILocalTextRegistry>();
            foreach (var pair in target)
                registry.Add(languageID, pair.Key, pair.Value);
        }
Exemple #3
0
        /// <summary>
        /// Adds translation from a hierarchical local text dictionary parsed from JSON file.
        /// </summary>
        /// <param name="nested">Object parsed from local text JSON string</param>
        /// <param name="prefix">Prefix to prepend before local text keys</param>
        /// <param name="languageID">Language ID</param>
        /// <param name="registry">Registry</param>
        public static void AddFromNestedDictionary(IDictionary <string, JToken> nested, string prefix, string languageID, ILocalTextRegistry registry = null)
        {
            if (nested == null)
            {
                throw new ArgumentNullException("nested");
            }

            var target = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            ProcessNestedDictionary(nested, prefix, target);

            registry = registry ?? Dependency.Resolve <ILocalTextRegistry>();
            foreach (var pair in target)
            {
                registry.Add(languageID, pair.Key, pair.Value);
            }
        }
Exemple #4
0
 /// <summary>
 /// Adds a local text entry to the registry
 /// </summary>
 /// <param name="languageID">Language ID (e.g. en-US, tr-TR)</param>
 /// <param name="key">Local text key</param>
 /// <param name="text">Translated text</param>
 public void Add(string languageID, string key, string text)
 {
     localTextRegistry.Add(languageID, key, text);
 }