diff --git a/Assets/Fungus/Flowchart/Editor/LanguageEditor.cs b/Assets/Fungus/Flowchart/Editor/LanguageEditor.cs index e735c81b..c9060f80 100644 --- a/Assets/Fungus/Flowchart/Editor/LanguageEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/LanguageEditor.cs @@ -34,23 +34,39 @@ namespace Fungus ExportLocalizationFile(t); } + if (GUILayout.Button(new GUIContent("Export Standard Text"))) + { + ExportStandardText(t); + } + serializedObject.ApplyModifiedProperties(); } public virtual void ExportLocalizationFile(Language language) { - string path = EditorUtility.SaveFilePanel("Export strings", "", + string path = EditorUtility.SaveFilePanel("Export Localization File", "", "localization.csv", ""); if (path.Length == 0) { return; } - Debug.Log(path); - - string csvData = language.ExportLocalizationFile(); + string csvData = language.GetCSVData(); File.WriteAllText(path, csvData); } + + public virtual void ExportStandardText(Language language) + { + string path = EditorUtility.SaveFilePanel("Export Standard Text", "", + "standard.txt", ""); + if (path.Length == 0) + { + return; + } + + string textData = language.GetStandardText(); + File.WriteAllText(path, textData); + } } } diff --git a/Assets/Fungus/Flowchart/Scripts/Language.cs b/Assets/Fungus/Flowchart/Scripts/Language.cs index 9d4af892..bfdccf33 100644 --- a/Assets/Fungus/Flowchart/Scripts/Language.cs +++ b/Assets/Fungus/Flowchart/Scripts/Language.cs @@ -46,9 +46,9 @@ namespace Fungus } /** - * Export all localized strings to an easy to edit CSV file. + * Convert all language items and localized strings to an easy to edit CSV format. */ - public virtual string ExportLocalizationFile() + public virtual string GetCSVData() { // Collect all the language items present in the scene Dictionary languageItems = FindLanguageItems(); @@ -57,7 +57,7 @@ namespace Fungus if (localizationFile != null && localizationFile.text.Length > 0) { - AddLocalisedStrings(languageItems, localizationFile.text); + AddLocalizedStrings(languageItems, localizationFile.text); } // Build CSV header row and a list of the language codes currently in use @@ -103,6 +103,9 @@ namespace Fungus return csvData; } + /** + * Buidls a dictionary of localizable objects in the scene. + */ protected Dictionary FindLanguageItems() { Dictionary languageItems = new Dictionary(); @@ -110,10 +113,12 @@ namespace Fungus // Export all character names foreach (Character character in GameObject.FindObjectsOfType()) { + // String id for character names is CHARACTER. LanguageItem languageItem = new LanguageItem(); languageItem.standardText = character.nameText; languageItem.description = character.description; - languageItems["CHARACTER." + character.nameText] = languageItem; + string stringId = "CHARACTER." + character.nameText; + languageItems[stringId] = languageItem; } // Export all Say and Menu commands in the scene @@ -132,24 +137,30 @@ namespace Fungus { foreach (Command command in block.commandList) { - string stringID = ""; + string stringId = ""; string standardText = ""; string description = ""; System.Type type = command.GetType(); if (type == typeof(Say)) { - stringID = "SAY." + flowchart.localizationId + "." + command.itemId; + // String id for Say commands is SAY... Say sayCommand = command as Say; standardText = sayCommand.storyText; description = sayCommand.description; + stringId = "SAY." + flowchart.localizationId + "." + sayCommand.itemId + "."; + if (sayCommand.character != null) + { + stringId += sayCommand.character.nameText; + } } else if (type == typeof(Menu)) { - stringID = "MENU." + flowchart.localizationId + "." + command.itemId; + // String id for Menu commands is MENU.. Menu menuCommand = command as Menu; standardText = menuCommand.text; description = menuCommand.description; + stringId = "MENU." + flowchart.localizationId + "." + menuCommand.itemId; } else { @@ -157,14 +168,14 @@ namespace Fungus } LanguageItem languageItem = null; - if (languageItems.ContainsKey(stringID)) + if (languageItems.ContainsKey(stringId)) { - languageItem = languageItems[stringID]; + languageItem = languageItems[stringId]; } else { languageItem = new LanguageItem(); - languageItems[stringID] = languageItem; + languageItems[stringId] = languageItem; } // Update basic properties,leaving localised strings intact @@ -177,7 +188,10 @@ namespace Fungus return languageItems; } - protected virtual void AddLocalisedStrings(Dictionary languageItems, string csvData) + /** + * Adds localized strings from CSV file data to a dictionary of language items in the scene. + */ + protected virtual void AddLocalizedStrings(Dictionary languageItems, string csvData) { CsvParser csvParser = new CsvParser(); string[][] csvTable = csvParser.Parse(csvData); @@ -226,6 +240,10 @@ namespace Fungus } } + /** + * Scan a localization CSV file and copies the strings for the specified language code + * into the text properties of the appropriate scene objects. + */ public virtual void SetActiveLanguage(string languageCode, string csvData) { if (!Application.isPlaying) @@ -299,15 +317,18 @@ namespace Fungus if (languageEntry.Length > 0) { localizedStrings[stringId] = languageEntry; - PopulateGameString(stringId, languageEntry, flowchartDict, characterDict); + PopulateTextProperty(stringId, languageEntry, flowchartDict, characterDict); } } } - public virtual void PopulateGameString(string stringId, - string localizedText, - Dictionary flowchartDict, - Dictionary characterDict) + /** + * Populates the text property of a single scene object with localized text. + */ + public virtual void PopulateTextProperty(string stringId, + string localizedText, + Dictionary flowchartDict, + Dictionary characterDict) { string[] idParts = stringId.Split('.'); if (idParts.Length == 0) @@ -318,11 +339,11 @@ namespace Fungus string stringType = idParts[0]; if (stringType == "SAY") { - if (idParts.Length != 3) + if (idParts.Length != 4) { return; } - + string flowchartId = idParts[1]; if (!flowchartDict.ContainsKey(flowchartId)) { @@ -390,6 +411,32 @@ namespace Fungus } } } + + /** + * Returns all standard text for SAY & MENU commands in the scene using an + * easy to edit custom text format. + */ + public virtual string GetStandardText() + { + // Collect all the language items present in the scene + Dictionary languageItems = FindLanguageItems(); + + string textData = ""; + foreach (string stringId in languageItems.Keys) + { + if (!stringId.StartsWith("SAY.") && !(stringId.StartsWith("MENU."))) + { + continue; + } + + LanguageItem languageItem = languageItems[stringId]; + + textData += "#" + stringId + "\n"; + textData += languageItem.standardText.Trim() + "\n\n"; + } + + return textData; + } } } \ No newline at end of file