예제 #1
0
        static public void ExportToPO(string directory, Project project, List <Dialogue> dialogues, Language language, bool workstringOnly, bool workstringFallback)
        {
            string path = Path.Combine(directory, "Dialogues.po");

            var  locas     = new Dictionary <string, POEntry>();
            bool isDefault = workstringOnly || (project.GetDefaultLanguage() == language);

            //Fill existing locas

            /*if (File.Exists(path))
             * {
             *  using (System.IO.StreamReader file = new System.IO.StreamReader(path, Encoding.UTF8))
             *  {
             *      while (!file.EndOfStream)
             *      {
             *          string lineId = file.ReadLine();
             *          if (lineId.StartsWith("msgid"))
             *          {
             *              lineId = lineId.Replace("msgid", "");
             *              lineId = lineId.Trim();
             *              lineId = lineId.Substring(1);
             *              lineId = lineId.Substring(0, lineId.Length - 1);
             *
             *              string lineStr = file.ReadLine();
             *              if (lineStr.StartsWith("msgstr"))
             *              {
             *                  lineStr = lineStr.Replace("msgstr", "");
             *                  lineStr = lineStr.Trim();
             *                  lineStr = lineStr.Substring(1);
             *                  lineStr = lineStr.Substring(0, lineStr.Length - 1);
             *
             *                  if (!locas.ContainsKey(lineId))
             *                      locas.Add(lineId, new POEntry() { text = lineStr });
             *              }
             *          }
             *      }
             *  }
             * }*/

            //Parse texts to localize and fill nodes references
            foreach (Dialogue dialogue in dialogues)
            {
                var orderedListNodes = new List <DialogueNode>();
                dialogue.GetOrderedNodes(ref orderedListNodes);
                foreach (DialogueNode dialogueNode in orderedListNodes)
                {
                    if (dialogueNode is DialogueNodeSentence || dialogueNode is DialogueNodeReply)
                    {
                        string workstring;
                        if (dialogueNode is DialogueNodeSentence)
                        {
                            workstring = (dialogueNode as DialogueNodeSentence).Sentence;
                        }
                        else
                        {
                            workstring = (dialogueNode as DialogueNodeReply).Reply;
                        }

                        POEntry entry;
                        if (!locas.TryGetValue(workstring, out entry))
                        {
                            string localizedText = workstring;
                            if (!workstringOnly)
                            {
                                TranslationEntry translation = dialogue.Translations.GetNodeEntry(dialogueNode, language);
                                if (translation == null)
                                {
                                    if (!workstringFallback)
                                    {
                                        localizedText = "";
                                    }
                                }
                                else
                                {
                                    localizedText = translation.Text;
                                }
                            }

                            localizedText = EditorHelper.FormatTextEntry(localizedText, language);  //language = workstring if workstringOnly

                            entry = new POEntry()
                            {
                                text = localizedText
                            };
                            locas.Add(workstring, entry);
                        }

                        entry.nodes.Add(EditorHelper.GetPrettyNodeID(dialogue, dialogueNode));
                    }
                }
            }

            //Re-write file
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, false, Encoding.UTF8))
            {
                string date = Utility.GetDateAsString(Utility.GetCurrentTime());

                //Header
                file.WriteLine("# Dialogues " + language.Name + " translation.");
                file.WriteLine("# ");
                file.WriteLine("msgid \"\"");
                file.WriteLine("msgstr \"\"");
                file.WriteLine("\"Project-Id-Version: Dialogues\\n\"");
                file.WriteLine("\"POT-Creation-Date: " + date + "\\n\"");
                file.WriteLine("\"PO-Revision-Date: " + date + "\\n\"");
                file.WriteLine("\"Language-Team: \\n\"");
                file.WriteLine("\"Language: " + language.LocalizationCode + "\\n\"");
                file.WriteLine("\"MIME-Version: 1.0\\n\"");
                file.WriteLine("\"Content-Type: text/plain; charset=UTF-8\\n\"");
                file.WriteLine("\"Content-Transfer-Encoding: 8bit\\n\"");
                file.WriteLine("\"Plural-Forms: nplurals=2; plural=(n != 1);\\n\"");
            }

            //Write entries
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true, Encoding.UTF8))
            {
                foreach (var kvp in locas)
                {
                    if (kvp.Value.nodes.Count > 0)
                    {
                        file.WriteLine("");
                        foreach (string node in kvp.Value.nodes)
                        {
                            file.WriteLine(String.Format("#: {0}", node));
                        }
                        file.WriteLine(String.Format("msgid \"{0}\"", kvp.Key));
                        file.WriteLine(String.Format("msgstr \"{0}\"", kvp.Value.text));
                    }
                }
            }
        }