From e292fada87f7094173be2761c3935386e7951d03 Mon Sep 17 00:00:00 2001 From: Creeper Lv Date: Mon, 2 Aug 2021 01:09:48 +0800 Subject: [PATCH] The 0.0.1.0 release of CLUNL.Localization. --- CLUNL.Localization/CLUNL.Localization.csproj | 11 ++- CLUNL.Localization/Language.cs | 77 +++++++++++++++----- 2 files changed, 67 insertions(+), 21 deletions(-) diff --git a/CLUNL.Localization/CLUNL.Localization.csproj b/CLUNL.Localization/CLUNL.Localization.csproj index 9f5c4f4..25588be 100644 --- a/CLUNL.Localization/CLUNL.Localization.csproj +++ b/CLUNL.Localization/CLUNL.Localization.csproj @@ -1,7 +1,16 @@ - + netstandard2.0 + True + 0.0.1.0 + https://github.com/creeperlv/CLUNL + https://github.com/creeperlv/CLUNL + Localization processing part of Creeper Lv's Universal dotNet Library + Creeper Lv + Creeper Lv + Copyright (C) 2020-2021 Creeper Lv + First version of CLUNL.Localization. diff --git a/CLUNL.Localization/Language.cs b/CLUNL.Localization/Language.cs index e1438b2..c1a6c5c 100644 --- a/CLUNL.Localization/Language.cs +++ b/CLUNL.Localization/Language.cs @@ -5,8 +5,14 @@ namespace CLUNL.Localization { + /// + /// An interface for objects that will use Language. + /// public interface ILocalized { + /// + /// Apply current language. + /// void ApplyLanguage(); } /// @@ -21,7 +27,16 @@ public class Language private const string GeneratorPrefix = "; Generated By CLUNL.Localization"; static Dictionary LanguageStrings = new Dictionary(); static bool isInited = false; + /// + /// If current language object is initialized with language files. + /// + /// public static bool IsInited() => isInited; + /// + /// Init languages with given settings file and a product name. + /// + /// + /// public static void Init(string SettingsFileName, string ProductName) { Init(ObtainInstalled(SettingsFileName, ProductName)); @@ -67,6 +82,10 @@ string LoadFromFile(string p) } return configuration; } + /// + /// Initialize language with given language code, eg: en-US. + /// + /// public static void Init(string PreferredLanguageCode) { isInited = true; @@ -80,6 +99,10 @@ public static void Init(string PreferredLanguageCode) if (File.Exists(langfile)) LoadLanguageFile(langfile); } + /// + /// Save current definitions into a file. + /// + /// public static void SaveCurrentDefinition(FileInfo TargetFile) { TargetFile.Delete(); @@ -94,30 +117,19 @@ public static void SaveCurrentDefinition(FileInfo TargetFile) } sw.Close(); } + /// + /// Load a language file and merge it into current definitions. + /// + /// public static void LoadLanguageFile(string FilePath) { var contents = File.ReadAllLines(FilePath); - foreach (var item in contents) - { - var pitem = item.Trim(); - if (pitem.StartsWith("#") || pitem.StartsWith(";") || pitem.StartsWith("//")) - { - continue; - } - else - { - var index = pitem.IndexOf(EqualSymbol); - if (index > 0) - { - var pairName = pitem.Substring(0, index); - var pairContent = pitem.Substring(index + 1); - if (!LanguageStrings.ContainsKey(pairName)) - LanguageStrings.Add(pairName, pairContent); - else LanguageStrings[pairName] = pairContent; - } - } - } + LoadFromStringArray(contents); } + /// + /// Load a language from a string array and merge it into current definitions. + /// + /// public static void LoadFromStringArray(string [] contents) { foreach (var item in contents) @@ -141,6 +153,10 @@ public static void LoadFromStringArray(string [] contents) } } } + /// + /// Load a language from a string and merge it into current definitions. + /// + /// public static void LoadFromString(string content) { using (StringReader stringReader = new StringReader(content)) @@ -168,16 +184,37 @@ public static void LoadFromString(string content) } } } + /// + /// Find a string with given fallback using string.Format(...). + /// + /// + /// + /// + /// public static string FindF(string Key, string Fallback = "", params string[] parameters) { var os = Find(Key, Fallback); return string.Format(os, parameters); } + + /// + /// Find a string with given fallback using string.Format(...). + /// + /// + /// + /// + /// public static string FindF(string Key, string Fallback = "", params object[] parameters) { var os = Find(Key, Fallback); return string.Format(os, parameters); } + /// + /// Find a string with given fallback. + /// + /// + /// + /// public static string Find(string Key, string Fallback = "") { if (!LanguageStrings.ContainsKey(Key))