Skip to content

Commit

Permalink
The 0.0.1.0 release of CLUNL.Localization.
Browse files Browse the repository at this point in the history
  • Loading branch information
creeperlv committed Aug 1, 2021
1 parent d5ef7bb commit e292fad
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 21 deletions.
11 changes: 10 additions & 1 deletion CLUNL.Localization/CLUNL.Localization.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>0.0.1.0</Version>
<PackageProjectUrl>https://github.com/creeperlv/CLUNL</PackageProjectUrl>
<RepositoryUrl>https://github.com/creeperlv/CLUNL</RepositoryUrl>
<Description>Localization processing part of Creeper Lv's Universal dotNet Library</Description>
<Authors>Creeper Lv</Authors>
<Company>Creeper Lv</Company>
<Copyright>Copyright (C) 2020-2021 Creeper Lv</Copyright>
<PackageReleaseNotes>First version of CLUNL.Localization.</PackageReleaseNotes>
</PropertyGroup>

</Project>
77 changes: 57 additions & 20 deletions CLUNL.Localization/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@

namespace CLUNL.Localization
{
/// <summary>
/// An interface for objects that will use Language.
/// </summary>
public interface ILocalized
{
/// <summary>
/// Apply current language.
/// </summary>
void ApplyLanguage();
}
/// <summary>
Expand All @@ -21,7 +27,16 @@ public class Language
private const string GeneratorPrefix = "; Generated By CLUNL.Localization";
static Dictionary<string, string> LanguageStrings = new Dictionary<string, string>();
static bool isInited = false;
/// <summary>
/// If current language object is initialized with language files.
/// </summary>
/// <returns></returns>
public static bool IsInited() => isInited;
/// <summary>
/// Init languages with given settings file and a product name.
/// </summary>
/// <param name="SettingsFileName"></param>
/// <param name="ProductName"></param>
public static void Init(string SettingsFileName, string ProductName)
{
Init(ObtainInstalled(SettingsFileName, ProductName));
Expand Down Expand Up @@ -67,6 +82,10 @@ string LoadFromFile(string p)
}
return configuration;
}
/// <summary>
/// Initialize language with given language code, eg: en-US.
/// </summary>
/// <param name="PreferredLanguageCode"></param>
public static void Init(string PreferredLanguageCode)
{
isInited = true;
Expand All @@ -80,6 +99,10 @@ public static void Init(string PreferredLanguageCode)
if (File.Exists(langfile))
LoadLanguageFile(langfile);
}
/// <summary>
/// Save current definitions into a file.
/// </summary>
/// <param name="TargetFile"></param>
public static void SaveCurrentDefinition(FileInfo TargetFile)
{
TargetFile.Delete();
Expand All @@ -94,30 +117,19 @@ public static void SaveCurrentDefinition(FileInfo TargetFile)
}
sw.Close();
}
/// <summary>
/// Load a language file and merge it into current definitions.
/// </summary>
/// <param name="FilePath"></param>
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);
}
/// <summary>
/// Load a language from a string array and merge it into current definitions.
/// </summary>
/// <param name="contents"></param>
public static void LoadFromStringArray(string [] contents)
{
foreach (var item in contents)
Expand All @@ -141,6 +153,10 @@ public static void LoadFromStringArray(string [] contents)
}
}
}
/// <summary>
/// Load a language from a string and merge it into current definitions.
/// </summary>
/// <param name="content"></param>
public static void LoadFromString(string content)
{
using (StringReader stringReader = new StringReader(content))
Expand Down Expand Up @@ -168,16 +184,37 @@ public static void LoadFromString(string content)
}
}
}
/// <summary>
/// Find a string with given fallback using string.Format(...).
/// </summary>
/// <param name="Key"></param>
/// <param name="Fallback"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static string FindF(string Key, string Fallback = "", params string[] parameters)
{
var os = Find(Key, Fallback);
return string.Format(os, parameters);
}

/// <summary>
/// Find a string with given fallback using string.Format(...).
/// </summary>
/// <param name="Key"></param>
/// <param name="Fallback"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static string FindF(string Key, string Fallback = "", params object[] parameters)
{
var os = Find(Key, Fallback);
return string.Format(os, parameters);
}
/// <summary>
/// Find a string with given fallback.
/// </summary>
/// <param name="Key"></param>
/// <param name="Fallback"></param>
/// <returns></returns>
public static string Find(string Key, string Fallback = "")
{
if (!LanguageStrings.ContainsKey(Key))
Expand Down

0 comments on commit e292fad

Please sign in to comment.