diff options
| author | william341 <[email protected]> | 2017-05-28 12:37:00 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-05-28 12:37:00 -0700 |
| commit | 771c20cfb3a703e0f1550fdcf9eb07b78298c944 (patch) | |
| tree | 59cb532e15ebff313fdba2be264d78ec0033f407 /ShiftOS_TheReturn/Localization.cs | |
| parent | 496b0cbf8659c99203f48210fd39c572400ae623 (diff) | |
| parent | c7ba7d733c756d196f98dd4533289a1ef4db715f (diff) | |
| download | shiftos_thereturn-771c20cfb3a703e0f1550fdcf9eb07b78298c944.tar.gz shiftos_thereturn-771c20cfb3a703e0f1550fdcf9eb07b78298c944.tar.bz2 shiftos_thereturn-771c20cfb3a703e0f1550fdcf9eb07b78298c944.zip | |
Merge pull request #1 from shiftos-game/master
welp, no longer a dev.
Diffstat (limited to 'ShiftOS_TheReturn/Localization.cs')
| -rw-r--r-- | ShiftOS_TheReturn/Localization.cs | 118 |
1 files changed, 58 insertions, 60 deletions
diff --git a/ShiftOS_TheReturn/Localization.cs b/ShiftOS_TheReturn/Localization.cs index de5e158..8adfa5a 100644 --- a/ShiftOS_TheReturn/Localization.cs +++ b/ShiftOS_TheReturn/Localization.cs @@ -33,10 +33,12 @@ using System.Threading.Tasks; namespace ShiftOS.Engine { + //define a whole bunch of things that are needed public interface ILanguageProvider { List<string> GetJSONTranscripts(); void WriteDefaultTranscript(); + void WriteTranscript(); string GetCurrentTranscript(); string[] GetAllLanguages(); } @@ -44,19 +46,21 @@ namespace ShiftOS.Engine public static class Localization { private static ILanguageProvider _provider = null; + private static string _languageid = null; public static string[] GetAllLanguages() { if(_provider == null) { - return JsonConvert.DeserializeObject<string[]>(Properties.Resources.languages); + return JsonConvert.DeserializeObject<string[]>(Properties.Resources.languages); //collect all the languages availible } else { - return _provider.GetAllLanguages(); + return _provider.GetAllLanguages(); //also collect all the languages avalible but from a specific provider this time } } + //if no local selected, english will be loaded public static void SetupTHETRUEDefaultLocals() { if (_provider == null) @@ -65,12 +69,19 @@ namespace ShiftOS.Engine var path = "english.local"; Utils.WriteAllText(Paths.GetPath(path), lines); } + else if (SaveSystem.CurrentSave == null) + { + var lines = Properties.Resources.strings_en; + var path = "english.local"; + Utils.WriteAllText(Paths.GetPath(path), lines); + } else { - _provider.WriteDefaultTranscript(); + _provider.WriteTranscript(); } } + // ignore this not really setup of default no no zone public static void SetupDefaultLocals(string lines, string path) { Utils.WriteAllText(Paths.GetPath(path), lines); @@ -78,13 +89,8 @@ namespace ShiftOS.Engine } - /// <summary> - /// Takes in a string and parses localization blocks into text blocks in the current language. - /// </summary> - /// <example>"{CODEPOINTS}: 0" will come out as "Codepoints: 0" if the current language is english.</example> - /// <param name="original">The string to parse</param> - /// <returns>The parsed string.</returns> - /// + // Takes in a string and parses localization blocks into text blocks in the current language. + // example: "{CODEPOINTS}: 0" will come out as "Codepoints: 0" if the current language is english public static string Parse(string original) { return Parse(original, new Dictionary<string, string>()); @@ -96,106 +102,98 @@ namespace ShiftOS.Engine Dictionary<string, string> localizationStrings = new Dictionary<string, string>(); - try { localizationStrings = JsonConvert.DeserializeObject<Dictionary<string, string>>(_provider.GetCurrentTranscript()); } catch { - localizationStrings = JsonConvert.DeserializeObject<Dictionary<string, string>>(Utils.ReadAllText(Paths.GetPath("english.local"))); - } - - foreach (var kv in localizationStrings) - { - original = original.Replace(kv.Key, kv.Value); + localizationStrings = JsonConvert.DeserializeObject<Dictionary<string, string>>(Utils.ReadAllText(Paths.GetPath("english.local"))); //if no provider fall back to english } - List<string> orphaned = new List<string>(); - if (Utils.FileExists("0:/dev_orphaned_lang.txt")) + foreach (var kv in localizationStrings.Where(x=>original.Contains(x.Key))) { - orphaned = JsonConvert.DeserializeObject<List<string>>(Utils.ReadAllText("0:/dev_orphaned_lang.txt")); + original = original.Replace(kv.Key, kv.Value); // goes through and replaces all the localization blocks } + //string original2 = Parse(original); - int start_index = 0; - int length = 0; - bool indexing = false; + string usernameReplace = ""; + string domainReplace = ""; - foreach (var c in original) + // if the user has saved then store their username and systemname in these string variables please + if (SaveSystem.CurrentSave != null) { - if (c == '{') + try { - start_index = original.IndexOf(c); - indexing = true; + usernameReplace = SaveSystem.CurrentUser.Username; } - - if (indexing == true) + catch { - length++; - if (c == '}') - { - indexing = false; - string o = original.Substring(start_index, length); - if (!orphaned.Contains(o)) - { - orphaned.Add(o); - } - start_index = 0; - length = 0; - } + usernameReplace = "user"; } - } - - if (orphaned.Count > 0) - { - Utils.WriteAllText("0:/dev_orphaned_lang.txt", JsonConvert.SerializeObject(orphaned, Formatting.Indented)); - } - //string original2 = Parse(original); - - string usernameReplace = ""; - string domainReplace = ""; - - if (SaveSystem.CurrentSave != null) - { - usernameReplace = SaveSystem.CurrentSave.Username; - domainReplace = SaveSystem.CurrentSave.SystemName; + try + { + domainReplace = SaveSystem.CurrentSave.SystemName; + } + catch + { + domainReplace = "system"; + } + } string namespaceReplace = ""; string commandReplace = ""; + // if the user did a command in the terminal and it had a period in it then split it up into the part before the period and the part after and then store them into these two string variables please if (TerminalBackend.latestCommmand != "" && TerminalBackend.latestCommmand.IndexOf('.') > -1) { namespaceReplace = TerminalBackend.latestCommmand.Split('.')[0]; commandReplace = TerminalBackend.latestCommmand.Split('.')[1]; } + // if you see these then replace them with what you need to Dictionary<string, string> defaultReplace = new Dictionary<string, string>() { {"%username", usernameReplace}, {"%domain", domainReplace}, {"%ns", namespaceReplace}, {"%cmd", commandReplace}, - {"%cp", SaveSystem.CurrentSave?.Codepoints.ToString() }, +#if LOCALIZE_CODEPOINTS + { "%cp", SaveSystem.CurrentSave?.Codepoints.ToString() }, +#endif }; - foreach (KeyValuePair<string, string> replacement in replace) + // actually do the replacement + foreach (KeyValuePair<string, string> replacement in replace.Where(x => original.Contains(x.Key))) { original = original.Replace(replacement.Key, Parse(replacement.Value)); } - foreach (KeyValuePair<string, string> replacement in defaultReplace) + // do the replacement but default + foreach (KeyValuePair<string, string> replacement in defaultReplace.Where(x => original.Contains(x.Key))) { original = original.Replace(replacement.Key, replacement.Value); } - return original; + return original; // returns the now replaced string } + // a few things are defined here public static void RegisterProvider(ILanguageProvider p) { _provider = p; } + + public static void SetLanguageID(string id) + { + _languageid = id; + } + + public static string GetLanguageID() + { + return _languageid; + } } } |
