Example #1
0
    /// <summary>
    /// Method to write all not-yet-translated keys as well as already translated ones to a file for translation.
    /// </summary>
    public static void WriteTranslationsToFile(Dictionary <string, string> translationsToWrite)
    {
#if UNITY_EDITOR
        if (translationsToWrite == null)
        {
            log.Error(_Logger.User.Msaw, "WriteTranslationsToFile() failed because given translationsToWrite was null. Not writing anything.");
            return;
        }

        DictHolder dh = new DictHolder();
        dh.Translations = translationsToWrite;

        using (System.IO.StreamWriter singleProfileFile = new System.IO.StreamWriter(CurrentLanguageFileResources, false, new UTF8Encoding(false)))
        {
#if removeJsonFX
            System.Text.StringBuilder jsonLine = new System.Text.StringBuilder();
            JsonFx.Json.JsonWriter    writer   = new JsonFx.Json.JsonWriter(jsonLine);
            writer.Write(dh);
            singleProfileFile.WriteLine(jsonLine.ToString());
#else
            StringBuilder sb = new StringBuilder(translationsToWrite.Count * 80);             // assumption
            sb.Append("{\n    \"Translations\": {\n");
            bool firstLine = true;
            foreach (KeyValuePair <string, string> kvp in translationsToWrite.OrderBy(o => o.Key))
            {
                if (firstLine == false)
                {
                    sb.Append(",\n");
                }
                sb.Append("        \"");
                sb.Append(kvp.Key);
                sb.Append("\": \"");
                sb.Append(kvp.Value.Replace("\n", "\\n").Replace("\"", "\\\""));
                sb.Append("\"");
                firstLine = false;
            }
            sb.Append("\n     }\n}");
            if (log.IsDebugEnabled)
            {
                log.Debug(_Logger.User.Msaw, "Writing loca to " + CurrentLanguageFileResources + ": " + sb.ToString());
            }
            singleProfileFile.WriteLine(sb.ToString());
#endif
        }
#endif
    }
Example #2
0
    /// <summary>
    /// Method that is necessary to avoid reflection and thus allow to use code stripping to reduce filesize.
    /// </summary>
    private static void ParseJSON(string jsonLine, Dictionary <string, string> translationDict)
    {
#if USE_MINIJSON_LEGACY
        int    indexSecondBraceOpen  = jsonLine.IndexOf("{", 1);
        int    indexSecondBraceClose = jsonLine.IndexOf("}");
        string cleanedJsonLine       = jsonLine.Substring(indexSecondBraceOpen + 1, indexSecondBraceClose - indexSecondBraceOpen - 1);
        //log.InfoJG("cleanedJsonLine = " + cleanedJsonLine);

        List <string> keys   = new List <string>();
        List <string> values = new List <string>();

        Hashtable hashTable2 = MiniJSONLegacy.jsonDecode("{" + cleanedJsonLine + "}") as Hashtable;
        if (hashTable2 != null)
        {
            foreach (object key in hashTable2.Keys)
            {
                keys.Add(key.ToString());
            }
            foreach (object value in hashTable2.Values)
            {
                values.Add(value.ToString());
            }
        }
        else
        {
            log.Error(Logger.User.Msaw, "Cannot parse JSON");
        }

        for (int i = 0; i < keys.Count; ++i)
        {
            translationDict.Add(keys[i], values[i]);
        }
#else
        DictHolder locaData = JsonConvert.DeserializeObject <DictHolder>(jsonLine);
        foreach (KeyValuePair <string, string> kvp in locaData.Translations)
        {
            translationDict.Add(kvp.Key, kvp.Value);
        }
#endif
    }