diff options
| author | Michael <[email protected]> | 2017-01-08 09:57:10 -0500 |
|---|---|---|
| committer | Michael <[email protected]> | 2017-01-08 09:57:10 -0500 |
| commit | f30dcf5ef41d54c588d7b42c48be8d941abba72e (patch) | |
| tree | 7705f99b965673b1c034ac2b1c56e65072c827df /ShiftOS_TheReturn/Localization.cs | |
| parent | 69dfad54724d4176dfce238a8d7e73970e6eef24 (diff) | |
| download | shiftos_thereturn-f30dcf5ef41d54c588d7b42c48be8d941abba72e.tar.gz shiftos_thereturn-f30dcf5ef41d54c588d7b42c48be8d941abba72e.tar.bz2 shiftos_thereturn-f30dcf5ef41d54c588d7b42c48be8d941abba72e.zip | |
Initial upload
Diffstat (limited to 'ShiftOS_TheReturn/Localization.cs')
| -rw-r--r-- | ShiftOS_TheReturn/Localization.cs | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/ShiftOS_TheReturn/Localization.cs b/ShiftOS_TheReturn/Localization.cs new file mode 100644 index 0000000..fd491b9 --- /dev/null +++ b/ShiftOS_TheReturn/Localization.cs @@ -0,0 +1,177 @@ +using Newtonsoft.Json; +using ShiftOS.Objects.ShiftFS; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShiftOS.Engine +{ + public interface ILanguageProvider + { + List<string> GetJSONTranscripts(); + void WriteDefaultTranscript(); + string GetCurrentTranscript(); + string[] GetAllLanguages(); + } + + public static class Localization + { + private static ILanguageProvider _provider = null; + + public static string[] GetAllLanguages() + { + if(_provider == null) + { + return JsonConvert.DeserializeObject<string[]>(Properties.Resources.languages); + } + else + { + return _provider.GetAllLanguages(); + } + } + + public static void SetupTHETRUEDefaultLocals() + { + if (_provider == null) + { + var lines = Properties.Resources.strings_en; + var path = "english.local"; + Utils.WriteAllText(Paths.GetPath(path), lines); + } + else + { + _provider.WriteDefaultTranscript(); + } + } + + public static void SetupDefaultLocals(string lines, string path) + { + Utils.WriteAllText(Paths.GetPath(path), lines); + + } + + + /// <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> + /// + public static string Parse(string original) + { + return Parse(original, new Dictionary<string, string>()); + } + + + public static string Parse(string original, Dictionary<string, string> replace) + { + 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); + } + + List<string> orphaned = new List<string>(); + if (Utils.FileExists("0:/dev_orphaned_lang.txt")) + { + orphaned = JsonConvert.DeserializeObject<List<string>>(Utils.ReadAllText("0:/dev_orphaned_lang.txt")); + } + + + int start_index = 0; + int length = 0; + bool indexing = false; + + foreach (var c in original) + { + if (c == '{') + { + start_index = original.IndexOf(c); + indexing = true; + } + + if (indexing == true) + { + length++; + if (c == '}') + { + indexing = false; + string o = original.Substring(start_index, length); + if (!orphaned.Contains(o)) + { + orphaned.Add(o); + } + start_index = 0; + length = 0; + } + } + } + + 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; + } + + string namespaceReplace = ""; + string commandReplace = ""; + + if (TerminalBackend.latestCommmand != "" && TerminalBackend.latestCommmand.IndexOf('.') > -1) + { + namespaceReplace = TerminalBackend.latestCommmand.Split('.')[0]; + commandReplace = TerminalBackend.latestCommmand.Split('.')[1]; + } + + Dictionary<string, string> defaultReplace = new Dictionary<string, string>() { + {"%username", usernameReplace}, + {"%domain", domainReplace}, + {"%ns", namespaceReplace}, + {"%cmd", commandReplace}, + {"%cp", SaveSystem.CurrentSave?.Codepoints.ToString() }, + }; + + foreach (KeyValuePair<string, string> replacement in replace) + { + original = original.Replace(replacement.Key, Parse(replacement.Value)); + } + + foreach (KeyValuePair<string, string> replacement in defaultReplace) + { + original = original.Replace(replacement.Key, replacement.Value); + } + + return original; + } + + public static void RegisterProvider(ILanguageProvider p) + { + _provider = p; + } + } +} |
