Beispiel #1
0
        public void should_insert_json_node()
        {
            var parent = new JsonNode(NodeType.Array, null);
            var node   = new JsonNode(NodeType.Object, null);

            parent.ShouldNotExecuteCallback <INode>((s, c) => s.Add(node, c));
            var child = parent.Cast <JsonNode>().First();

            child.Element.ShouldBeSameAs(node.Element);
            child.Parent.ShouldBeSameAs(parent);
        }
Beispiel #2
0
        /// <summary>
        /// Translates the specified key.
        /// </summary>
        /// <param name="key">The translation key.</param>
        /// <param name="parameters">The parameters.</param>
        /// /// <param name="count">The pluralization count.</param>
        /// <returns>The translated content matching the translation key.</returns>
        public string Translate(string key, Dictionary <string, string> parameters = null, int?count = null)
        {
            JsonNode result = translations;

            foreach (var part in key.Split('.'))
            {
                result = JsonNode.Cast(result, part);
            }

            if (result == null)
            {
                return(key);
            }

            var translation = result.ToString().Trim(' ', '\t', '\n', '\v', '\f', '\r', '"');

            if (count != null)
            {
                var pluralization = translation.Split('|');
                if (count <= 1 && translation.Length > 0 && pluralization.Length > 0)
                {
                    translation = pluralization[0];
                }
                else if (count > 1 && translation.Length > 1 && pluralization.Length > 1)
                {
                    translation = pluralization[1];
                }

                var inferiorRgx = new Regex(@"^\[\*,(\d+)\]", RegexOptions.IgnoreCase);
                var superiorRgx = new Regex(@"^\[(\d+),\*\]", RegexOptions.IgnoreCase);
                var equalRgx    = new Regex(@"^{(\d+)}", RegexOptions.IgnoreCase);
                var betweenRgx  = new Regex(@"^\[(\d+),(\d+)\]", RegexOptions.IgnoreCase);

                foreach (var testString in pluralization)
                {
                    if (inferiorRgx.IsMatch(testString) && count <= int.Parse(inferiorRgx.Matches(testString)[0].Groups[1].ToString()))
                    {
                        translation = inferiorRgx.Replace(testString, "");
                    }
                    if (superiorRgx.IsMatch(testString) && count >= int.Parse(superiorRgx.Matches(testString)[0].Groups[1].ToString()))
                    {
                        translation = superiorRgx.Replace(testString, "");
                    }
                    if (equalRgx.IsMatch(testString) && count == int.Parse(equalRgx.Matches(testString)[0].Groups[1].ToString()))
                    {
                        translation = equalRgx.Replace(testString, "");
                    }
                    if (!betweenRgx.IsMatch(testString))
                    {
                        continue;
                    }
                    var between = betweenRgx.Matches(testString)[0];
                    if (count >= int.Parse(between.Groups[1].ToString()) && count <= int.Parse(between.Groups[2].ToString()))
                    {
                        translation = betweenRgx.Replace(testString, "");
                    }
                }
            }

            if (parameters == null)
            {
                return(translation);
            }

            foreach (var index in parameters.Keys)
            {
                translation = translation.Replace(":" + index.ToLower(), parameters[index].ToLower());
                translation = translation.Replace(":" + char.ToUpper(index[0]) + index.Substring(1).ToLower(), char.ToUpper(parameters[index][0]) + parameters[index].Substring(1).ToLower());
                translation = translation.Replace(":" + index.ToUpper(), parameters[index].ToUpper());
            }

            return(translation);
        }