/// <summary>Performs the translation now.</summary>
        public void Translate()
        {
            // Start a request:
            XMLHttpRequest request = new XMLHttpRequest();

            request.open("post", "http://translate.kulestar.com/v2/machine");

            request.onerror = delegate(UIEvent e){
                // Failed:
                Errored(request.responseText);
            };

            request.onload = delegate(UIEvent e){
                // Parse the JSON:
                JSObject json    = JSON.Parse(request.responseText);
                JSArray  results = json == null ? null : (json["results"] as JSArray);

                if (results == null)
                {
                    // Failed:
                    Errored(request.responseText);
                }
                else
                {
                    // Let it know it completed:
                    Complete(results);
                }
            };

            // Send it off:
            request.send(Json);
        }
Exemple #2
0
        /// <summary>Called when the translation is complete.</summary>
        /// <param name="results">The translated result set.</param>
        public void Complete(JSArray results)
        {
            // Results is the assoc array. Build the XML now:
            bool          first   = true;
            StringBuilder builder = new StringBuilder();

            foreach (KeyValuePair <string, JSObject> kvp in results)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    builder.Append("\r\n");
                }

                builder.Append("<var name='" + kvp.Key + "'>\r\n\t");
                builder.Append(kvp.Value);
                builder.Append("\r\n</var>");
            }

            // Get the result:
            string result = builder.ToString();

            // Write it out now:
            File.WriteAllText(TargetPath, result);
        }
        /// <summary>Called when the translation is complete.</summary>
        /// <param name="translation">The translated result.</param>
        public void Complete(JSArray results)
        {
            // Results contains a block of one or more groups:
            foreach (KeyValuePair <string, JSObject> kvp in results)
            {
                // The group it's going into:
                GroupToTranslate target = Groups[int.Parse(kvp.Key)];

                // Pass through the value set:
                target.Complete(kvp.Value as JSArray);
            }

            if (OnComplete != null)
            {
                // Run the callback:
                OnComplete(this);

                // Refresh assets:
                AssetDatabase.Refresh();
            }
        }
        /// <summary>Reads a JSON array from the given parser.</summary>
        private static JSObject ReadObject(CodeLexer parser)
        {
            // Kick it off:
            char current = parser.Peek();

            // The read key:
            string key = "";
            // And one for the value:
            StringBuilder value       = new StringBuilder();
            bool          valueString = false;
            bool          inString    = false;
            char          stringChar  = '\0';
            // Is the current letter in "escaped" mode?
            bool escaped = false;

            JSObject result = null;

            while (current != StringReader.NULL)
            {
                if (inString)
                {
                    if (escaped)
                    {
                        value.Append(current);
                        escaped = false;
                    }
                    else if (current == stringChar)
                    {
                        // Exiting the string:
                        parser.Literal = false;
                        inString       = false;
                    }
                    else if (current == '\\')
                    {
                        // Escape:
                        escaped = true;
                    }
                    else
                    {
                        value.Append(current);
                    }
                }
                else if (current == '"' || current == '\'')
                {
                    // Track which it was:
                    stringChar = current;

                    // Entering the string:
                    parser.Literal = true;
                    inString       = true;
                    valueString    = true;
                }
                else if (current == '{' || current == '[')
                {
                    if (result != null)
                    {
                        // Reading an object:
                        JSObject arr = ReadObject(parser);

                        if (key != "")
                        {
                            result[key] = arr;
                        }
                        else
                        {
                            result.push(arr);
                        }

                        // Use up the key:
                        key = "";
                    }
                    else
                    {
                        result = new JSArray();
                    }
                }
                else if (current == '}' || current == ']' || current == ',')
                {
                    // Complete the value if we need to.
                    string val = value.ToString();
                    value.Length = 0;

                    if (!valueString && val == "null")
                    {
                        val = null;
                    }

                    valueString = false;

                    if (key != "")
                    {
                        result[key] = new JSValue(val);
                        key         = "";
                    }
                    else if (val != "")
                    {
                        result.push(val);
                    }

                    if (current != ',')
                    {
                        break;
                    }
                }
                else if (current == ':')
                {
                    // We just read a key:
                    key          = value.ToString();
                    value.Length = 0;
                    valueString  = false;
                }
                else
                {
                    // Add to value:
                    value.Append(current);
                }

                // Read it off:
                parser.Read();

                // Peek next one:
                current = parser.Peek();
            }

            if (value.Length != 0)
            {
                if (result == null)
                {
                    // Get the value string:
                    string val = value.ToString();

                    if (!valueString && val == "null")
                    {
                        // It was the word "null".
                        return(null);
                    }

                    result = new JSValue(val);
                }
                else
                {
                    // Malformed - missing closing bracket.
                    throw new Exception("JSON syntax error: Closing bracket missing.");
                }
            }

            return(result);
        }