Exemplo n.º 1
0
        private static LocalizationDictionary ResolveDictionary()
        {
            var dictionary = LocalizationDictionary.GetResources(LocalizationService.Current.Culture);

            if (dictionary is null)
            {
                throw new InvalidOperationException(
                          $"Pack for language {LocalizationService.Current.Culture} was not found");
            }

            return(dictionary);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        public static void RegisterPack(LocalizationDictionary item)
        {
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (LocalizationService.RegisteredPacks.ContainsKey(item.Culture))
            {
                return;
            }

            LocalizationService.RegisteredPacks.Add(item.Culture, item);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="cultureInfo"></param>
        /// <returns></returns>
        public static LocalizationDictionary GetResources(CultureInfo cultureInfo)
        {
            if (cultureInfo is null)
            {
                throw new ArgumentNullException(nameof(cultureInfo));
            }

            if (!LocalizationService.RegisteredPacks.ContainsKey(cultureInfo))
            {
                return(Default());
            }

            LocalizationDictionary dictionary = LocalizationService.RegisteredPacks[cultureInfo];

            return(dictionary);
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dictionary"></param>
        public static void RegisterPacks(StreamBase dictionary)
        {
            if (dictionary is null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            for (int i = 0; i < dictionary.Packs.Count; i++)
            {
                LocalizationDictionary item = dictionary.Packs[i];
                if (LocalizationService.RegisteredPacks.ContainsKey(item.Culture))
                {
                    continue;
                }

                LocalizationService.RegisteredPacks.Add(item.Culture, item);
            }
        }
Exemplo n.º 5
0
        protected override void OnLoad()
        {
            var fileJson = File.ReadAllText(Path);

            var a = fileJson.FromJson <Dictionary <string, object> >();

            if (a == null)
            {
                throw new FileFormatException("Json load failed");
            }

            var author = SetIfContains(a, "Author", "Anonymous");

            bool IsSinglePack = false;

            if (a["Type"] is string)
            {
                if (a["Type"].ToString().ToUpperInvariant() == "SINGLE")
                {
                    IsSinglePack = true;
                }
            }

            if (IsSinglePack)
            {
                var tmp = new LocalizationDictionary()
                {
                    Version     = SetIfContains(a, "Version"),
                    Author      = author,
                    EnglishName = SetIfContains(a, "EnglishName"),
                    CultureName = SetIfContains(a, "CultureName"),
                    CultureId   = SetIfContains(a, "Culture"),
                    RTL         = SetIfContains(a, "RTL", "FALSE").ToUpperInvariant() == "TRUE"
                };

                if (!(a["Data"] is List <object> data))
                {
                    return;
                }

                foreach (Dictionary <string, object> node in data)
                {
                    if (node == null)
                    {
                        continue;
                    }
                    var innerData = new Dictionary <string, string>();
                    foreach (var attribute in node)
                    {
                        if (attribute.Key.ToUpperInvariant() == "ID")
                        {
                            continue;
                        }
                        innerData[attribute.Key] = attribute.Value.ToString();
                    }

                    if (tmp.Data.ContainsKey(node["Id"].ToString()))
                    {
                        continue;
                    }

                    tmp.Data.Add(node["Id"].ToString(), innerData);
                }

                Packs.Add(tmp);
            }
            else
            {
                var version = SetIfContains(a, "Version");
                Dictionary <string, LocalizationDictionary> temps = new Dictionary <string, LocalizationDictionary>();

                if (!(a["Languages"] is List <object> langs))
                {
                    return;
                }

                foreach (Dictionary <string, object> node in langs)
                {
                    if (node == null)
                    {
                        continue;
                    }
                    var tmp = new LocalizationDictionary()
                    {
                        Version     = version,
                        EnglishName = SetIfContains(node, "EnglishName"),
                        CultureName = SetIfContains(node, "CultureName"),
                        Author      = author,
                        CultureId   = SetIfContains(node, "Culture"),
                        RTL         = SetIfContains(a, "RTL", "FALSE").ToUpperInvariant() == "TRUE"
                    };
                    temps.Add(tmp.CultureId, tmp);
                }

                if (!(a["Data"] is List <object> data))
                {
                    return;
                }

                foreach (var node in data)
                {
                    if (node == null)
                    {
                        continue;
                    }
                    if (!(data[0] is Dictionary <string, object> nodec))
                    {
                        return;
                    }
                    if (!(nodec["data"] is List <object> nodechild))
                    {
                        return;
                    }
                    foreach (Dictionary <string, object> attributes in nodechild)
                    {
                        var innerData = new Dictionary <string, Dictionary <string, string> >();
                        var currId    = 0;
                        foreach (var attribute in attributes)
                        {
                            if (attribute.Key.ToUpperInvariant() == "ID")
                            {
                                currId = Convert.ToInt32(attribute.Value, CultureInfo.InvariantCulture);

                                continue;
                            }
                            if (!(attribute.Value is Dictionary <string, object> child))
                            {
                                continue;
                            }

                            foreach (var item in child)
                            {
                                if (!innerData.ContainsKey(item.Key))
                                {
                                    innerData[item.Key] = new Dictionary <string, string>();
                                }

                                innerData[item.Key][attribute.Key] = item.Value.ToString();
                            }
                        }
                        foreach (var item in innerData)
                        {
                            var vals = item.Value;
                            if (!temps[item.Key].Data.ContainsKey($"{currId}"))
                            {
                                temps[item.Key].Data[$"{currId}"] = new Dictionary <string, string>();
                            }
                            foreach (var ia in vals)
                            {
                                temps[item.Key].Data[$"{currId}"].Add(ia.Key, ia.Value);
                            }
                        }
                    }
                }
                foreach (var item in temps.Values)
                {
                    Packs.Add(item);
                }
                temps.Clear();
            }
        }
Exemplo n.º 6
0
        protected override void OnLoad()
        {
            var xmlDocument = new XmlDocument()
            {
                XmlResolver = null
            };

            xmlDocument.Load(Path);
            var docElement = xmlDocument.DocumentElement;

            if (docElement == null)
            {
                return;
            }

            if (docElement.Name != "Pack")
            {
                throw new XmlException("Invalid root element. Must be Pack");
            }

            var tmp = new LocalizationDictionary()
            {
                Version     = docElement.Attributes["Version"]?.Value,
                Author      = docElement.Attributes["Author"]?.Value,
                EnglishName = docElement.Attributes["EnglishName"]?.Value,
                CultureName = docElement.Attributes["CultureName"]?.Value,
                CultureId   = docElement.Attributes["Culture"]?.Value,
            };

            foreach (XmlNode node in docElement.ChildNodes)
            {
                if (node.Name != "Value")
                {
                    continue;
                }

                var innerData = new Dictionary <string, string>();

                if (node.Attributes == null)
                {
                    continue;
                }

                foreach (XmlAttribute attribute in node.Attributes)
                {
                    if (attribute.Name == "Id")
                    {
                        if (tmp.Data.ContainsKey(attribute.Value))
                        {
                            continue;
                        }

                        tmp.Data[attribute.Value] = innerData;
                    }
                    else
                    {
                        innerData[attribute.Name] = attribute.Value;
                    }
                }
            }
            Packs.Add(tmp);
        }
Exemplo n.º 7
0
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values is null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (targetType is null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            if (parameter is null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            if (culture is null)
            {
                throw new ArgumentNullException(nameof(culture));
            }

            try
            {
                var parametersCount = _isStaticUid ? values.Length - 1 : values.Length - 2;
                if (string.IsNullOrEmpty(_uid))
                {
                    if (values[1] == null)
                    {
                        throw new ArgumentNullException(nameof(values));
                    }
                    _isStaticUid = false;
                    _uid         = values[1].ToString();
                    --parametersCount;
                }
                LocalizationDictionary dictionary = ResolveDictionary();
                var translatedObject = dictionary.Translate(_uid, _vid, _defaultValue, targetType);

                if (translatedObject == null || parametersCount == 0)
                {
                    return(translatedObject);
                }

                var parameters = new object[parametersCount];
                Array.Copy(values, values.Length - parametersCount, parameters, 0, parameters.Length);

                try
                {
                    translatedObject = string.Format(CultureInfo.InvariantCulture, translatedObject.ToString(), parameters);
                }
                catch (Exception)
                {
                    #region Trace

                    Debug.WriteLine($"LocalizationConverter failed to format text {translatedObject.ToString()}");

                    #endregion Trace

                    throw new LocalizationException(LocalizerError.FormattingFailed);
                }
                return(translatedObject);
            }
            catch (LocalizationException ex)
            {
                #region Trace

                Debug.WriteLine($"LocalizationConverter failed to convert text: {ex.Message}");

                #endregion Trace
            }
            return(null);
        }