From edf4aef6adf8a2a45c347f70804fc5ac93070576 Mon Sep 17 00:00:00 2001 From: Michael VanOverbeek Date: Fri, 7 Apr 2017 17:01:23 +0000 Subject: Merge --- ShiftOS_TheReturn/Localization.cs | 201 -------------------------------------- 1 file changed, 201 deletions(-) delete mode 100644 ShiftOS_TheReturn/Localization.cs (limited to 'ShiftOS_TheReturn/Localization.cs') diff --git a/ShiftOS_TheReturn/Localization.cs b/ShiftOS_TheReturn/Localization.cs deleted file mode 100644 index de5e158..0000000 --- a/ShiftOS_TheReturn/Localization.cs +++ /dev/null @@ -1,201 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -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 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(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); - - } - - - /// - /// Takes in a string and parses localization blocks into text blocks in the current language. - /// - /// "{CODEPOINTS}: 0" will come out as "Codepoints: 0" if the current language is english. - /// The string to parse - /// The parsed string. - /// - public static string Parse(string original) - { - return Parse(original, new Dictionary()); - } - - - public static string Parse(string original, Dictionary replace) - { - Dictionary localizationStrings = new Dictionary(); - - - - try - { - localizationStrings = JsonConvert.DeserializeObject>(_provider.GetCurrentTranscript()); - } - catch - { - localizationStrings = JsonConvert.DeserializeObject>(Utils.ReadAllText(Paths.GetPath("english.local"))); - } - - foreach (var kv in localizationStrings) - { - original = original.Replace(kv.Key, kv.Value); - } - - List orphaned = new List(); - if (Utils.FileExists("0:/dev_orphaned_lang.txt")) - { - orphaned = JsonConvert.DeserializeObject>(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 defaultReplace = new Dictionary() { - {"%username", usernameReplace}, - {"%domain", domainReplace}, - {"%ns", namespaceReplace}, - {"%cmd", commandReplace}, - {"%cp", SaveSystem.CurrentSave?.Codepoints.ToString() }, - }; - - foreach (KeyValuePair replacement in replace) - { - original = original.Replace(replacement.Key, Parse(replacement.Value)); - } - - foreach (KeyValuePair replacement in defaultReplace) - { - original = original.Replace(replacement.Key, replacement.Value); - } - - return original; - } - - public static void RegisterProvider(ILanguageProvider p) - { - _provider = p; - } - } -} -- cgit v1.2.3