diff --git a/Assets/Fungus/Narrative/Scripts/ILocalization.cs b/Assets/Fungus/Narrative/Scripts/ILocalization.cs new file mode 100644 index 00000000..0c03c30a --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/ILocalization.cs @@ -0,0 +1,80 @@ +using UnityEngine; + +namespace Fungus +{ + /// + /// Multi-language localization support. + /// + public interface ILocalization + { + /// + /// Language to use at startup, usually defined by a two letter language code (e.g DE = German). + /// + string ActiveLanguage { get; } + + /// + /// CSV file containing localization data which can be easily edited in a spreadsheet tool. + /// + TextAsset LocalizationFile { get; } + + /// + /// Stores any notification message from export / import methods. + /// + string NotificationText { get; set; } + + /// + /// Convert all text items and localized strings to an easy to edit CSV format. + /// + string GetCSVData(); + + /// + /// Scan a localization CSV file and copies the strings for the specified language code + /// into the text properties of the appropriate scene objects. + /// + void SetActiveLanguage(string languageCode, bool forceUpdateSceneText = false); + + /// + /// Populates the text property of a single scene object with a new text value. + /// + bool PopulateTextProperty(string stringId, string newText); + + /// + /// Returns all standard text for localizeable text in the scene using an + /// easy to edit custom text format. + /// + string GetStandardText(); + + /// + /// Sets standard text on scene objects by parsing a text data file. + /// + void SetStandardText(string textData); + } + + /// + /// An item of localizeable text. + /// + public interface ILocalizable + { + /// + /// Gets the standard (non-localized) text. + /// + string GetStandardText(); + + /// + /// Sets the standard (non-localized) text. + /// + /// Standard text. + void SetStandardText(string standardText); + + /// + /// Gets the description used to help localizers. + /// + /// The description. + string GetDescription(); + + /// + /// Gets the unique string identifier. + /// + string GetStringId(); + } +} diff --git a/Assets/Fungus/Narrative/Scripts/ILocalization.cs.meta b/Assets/Fungus/Narrative/Scripts/ILocalization.cs.meta new file mode 100644 index 00000000..665e95e0 --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/ILocalization.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 284ef4e7fb4054263999d7030c61a79c +timeCreated: 1473681760 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Narrative/Scripts/Localization.cs b/Assets/Fungus/Narrative/Scripts/Localization.cs index 8ef5c28c..6e8350eb 100644 --- a/Assets/Fungus/Narrative/Scripts/Localization.cs +++ b/Assets/Fungus/Narrative/Scripts/Localization.cs @@ -12,25 +12,13 @@ using Ideafixxxer.CsvParser; namespace Fungus { - public interface ILocalizable - { - string GetStandardText(); - void SetStandardText(string standardText); - string GetDescription(); - string GetStringId(); - } - /// /// Multi-language localization support. /// - public class Localization : MonoBehaviour, StringSubstituter.ISubstitutionHandler + public class Localization : MonoBehaviour, ILocalization, StringSubstituter.ISubstitutionHandler { - /// - /// Language to use at startup, usually defined by a two letter language code (e.g DE = German). - /// [Tooltip("Language to use at startup, usually defined by a two letter language code (e.g DE = German)")] [SerializeField] protected string activeLanguage = ""; - public virtual string ActiveLanguage { get { return activeLanguage; } } protected static Dictionary localizedStrings = new Dictionary(); @@ -46,18 +34,10 @@ namespace Fungus public Dictionary localizedStrings = new Dictionary(); } - /// - /// CSV file containing localization data which can be easily edited in a spreadsheet tool. - /// [Tooltip("CSV file containing localization data which can be easily edited in a spreadsheet tool")] [SerializeField] protected TextAsset localizationFile; - public virtual TextAsset LocalizationFile { get { return localizationFile; } set { localizationFile = value; } } - /// - /// Stores any notification message from export / import methods. - /// protected string notificationText = ""; - public virtual string NotificationText { get { return notificationText; } set { notificationText = value; } } protected bool initialized; @@ -85,7 +65,7 @@ namespace Fungus } } - public virtual void Start() + protected virtual void Start() { Init(); } @@ -112,25 +92,6 @@ namespace Fungus initialized = true; } - public virtual void ClearLocalizeableCache() - { - localizeableObjects.Clear(); - } - - // Build a cache of all the localizeable objects in the scene - protected virtual void CacheLocalizeableObjects() - { - UnityEngine.Object[] objects = Resources.FindObjectsOfTypeAll(typeof(Component)); - foreach (UnityEngine.Object o in objects) - { - ILocalizable localizable = o as ILocalizable; - if (localizable != null) - { - localizeableObjects[localizable.GetStringId()] = localizable; - } - } - } - /// /// Looks up the specified string in the localized strings table. /// For this to work, a localization file and active language must have been set previously. @@ -151,66 +112,18 @@ namespace Fungus return null; } - /// - /// Convert all text items and localized strings to an easy to edit CSV format. - /// - public virtual string GetCSVData() + // Build a cache of all the localizeable objects in the scene + protected virtual void CacheLocalizeableObjects() { - // Collect all the text items present in the scene - Dictionary textItems = FindTextItems(); - - // Update text items with localization data from CSV file - if (localizationFile != null && - localizationFile.text.Length > 0) - { - AddCSVDataItems(textItems, localizationFile.text); - } - - // Build CSV header row and a list of the language codes currently in use - string csvHeader = "Key,Description,Standard"; - List languageCodes = new List(); - foreach (TextItem textItem in textItems.Values) - { - foreach (string languageCode in textItem.localizedStrings.Keys) - { - if (!languageCodes.Contains(languageCode)) - { - languageCodes.Add(languageCode); - csvHeader += "," + languageCode; - } - } - } - - // Build the CSV file using collected text items - int rowCount = 0; - string csvData = csvHeader + "\n"; - foreach (string stringId in textItems.Keys) + UnityEngine.Object[] objects = Resources.FindObjectsOfTypeAll(typeof(Component)); + foreach (UnityEngine.Object o in objects) { - TextItem textItem = textItems[stringId]; - - string row = CSVSupport.Escape(stringId); - row += "," + CSVSupport.Escape(textItem.description); - row += "," + CSVSupport.Escape(textItem.standardText); - - foreach (string languageCode in languageCodes) + ILocalizable localizable = o as ILocalizable; + if (localizable != null) { - if (textItem.localizedStrings.ContainsKey(languageCode)) - { - row += "," + CSVSupport.Escape(textItem.localizedStrings[languageCode]); - } - else - { - row += ","; // Empty field - } + localizeableObjects[localizable.GetStringId()] = localizable; } - - csvData += row + "\n"; - rowCount++; } - - notificationText = "Exported " + rowCount + " localization text items."; - - return csvData; } /// @@ -333,10 +246,78 @@ 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. - /// + #region ILocalization interface + + public virtual string ActiveLanguage { get { return activeLanguage; } } + + public virtual TextAsset LocalizationFile { get { return localizationFile; } set { localizationFile = value; } } + + public virtual string NotificationText { get { return notificationText; } set { notificationText = value; } } + + public virtual void ClearLocalizeableCache() + { + localizeableObjects.Clear(); + } + + public virtual string GetCSVData() + { + // Collect all the text items present in the scene + Dictionary textItems = FindTextItems(); + + // Update text items with localization data from CSV file + if (localizationFile != null && + localizationFile.text.Length > 0) + { + AddCSVDataItems(textItems, localizationFile.text); + } + + // Build CSV header row and a list of the language codes currently in use + string csvHeader = "Key,Description,Standard"; + List languageCodes = new List(); + foreach (TextItem textItem in textItems.Values) + { + foreach (string languageCode in textItem.localizedStrings.Keys) + { + if (!languageCodes.Contains(languageCode)) + { + languageCodes.Add(languageCode); + csvHeader += "," + languageCode; + } + } + } + + // Build the CSV file using collected text items + int rowCount = 0; + string csvData = csvHeader + "\n"; + foreach (string stringId in textItems.Keys) + { + TextItem textItem = textItems[stringId]; + + string row = CSVSupport.Escape(stringId); + row += "," + CSVSupport.Escape(textItem.description); + row += "," + CSVSupport.Escape(textItem.standardText); + + foreach (string languageCode in languageCodes) + { + if (textItem.localizedStrings.ContainsKey(languageCode)) + { + row += "," + CSVSupport.Escape(textItem.localizedStrings[languageCode]); + } + else + { + row += ","; // Empty field + } + } + + csvData += row + "\n"; + rowCount++; + } + + notificationText = "Exported " + rowCount + " localization text items."; + + return csvData; + } + public virtual void SetActiveLanguage(string languageCode, bool forceUpdateSceneText = false) { if (!Application.isPlaying) @@ -417,10 +398,10 @@ namespace Fungus { continue; } - + string stringId = fields[0]; string languageEntry = CSVSupport.Unescape(fields[languageIndex]); - + if (languageEntry.Length > 0) { localizedStrings[stringId] = languageEntry; @@ -429,9 +410,6 @@ namespace Fungus } } - /// - /// Populates the text property of a single scene object with a new text value. - /// public virtual bool PopulateTextProperty(string stringId, string newText) { // Ensure that all localizeable objects have been cached @@ -451,10 +429,6 @@ namespace Fungus return false; } - /// - /// Returns all standard text for localizeable text in the scene using an - /// easy to edit custom text format. - /// public virtual string GetStandardText() { // Collect all the text items present in the scene @@ -472,13 +446,10 @@ namespace Fungus } notificationText = "Exported " + rowCount + " standard text items."; - + return textData; } - /// - /// Sets standard text on scene objects by parsing a text data file. - /// public virtual void SetStandardText(string textData) { string[] lines = textData.Split('\n'); @@ -523,10 +494,10 @@ namespace Fungus notificationText = "Updated " + updatedCount + " standard text items."; } - /// - /// Implementation of StringSubstituter.ISubstitutionHandler. - /// Replaces tokens of the form {$KeyName} with the localized value corresponding to that key. - /// + #endregion + + #region StringSubstituter.ISubstitutionHandler imlpementation + public virtual bool SubstituteStrings(StringBuilder input) { // This method could be called from the Start method of another component, so we @@ -555,5 +526,7 @@ namespace Fungus return modified; } + + #endregion } } \ No newline at end of file