-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(+) Added: LocalizatorBase (+) Added: GateLocalizator (~) Updated: StoredLocalizator
- Loading branch information
1 parent
efb874a
commit 54b6327
Showing
7 changed files
with
99 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Newtonsoft.Json; | ||
using SKitLs.Utils.Localizations.Languages; | ||
|
||
namespace SKitLs.Utils.Localizations.Model | ||
{ | ||
public class GateLocalizator : LocalizatorBase | ||
{ | ||
/// <summary> | ||
/// Determines the extension of localization resource files. | ||
/// </summary> | ||
public const string LocalExtension = ".json"; | ||
|
||
public GateLocalizator(string localsPath, LanguageCode defaultLanguage = LanguageCode.EN) : base(localsPath, defaultLanguage) | ||
{ } | ||
|
||
protected override string? InternalResolveString(LanguageCode? lang, string key, bool resolveDefault, params string?[] format) | ||
{ | ||
if (!Directory.Exists(LocalsPath)) | ||
Directory.CreateDirectory(LocalsPath); | ||
|
||
var langName = string.Empty; | ||
if (lang.HasValue) | ||
{ | ||
langName = lang.Value.ToString(); | ||
} | ||
else if (resolveDefault) | ||
{ | ||
langName = DefaultLanguage.ToString(); | ||
} | ||
else | ||
return null; | ||
|
||
var files = Directory.GetFiles(LocalsPath) | ||
.Select(x => new FileInfo(x)) | ||
.Where(x => x.Extension == LocalExtension) | ||
.Where(x => x.Name.StartsWith(langName, true, null)); | ||
|
||
foreach (var lFile in files) | ||
{ | ||
using var reader = new StreamReader(lFile.FullName); | ||
var json = reader.ReadToEnd(); | ||
var langCollection = JsonConvert.DeserializeObject<Dictionary<string, string>>(json) | ||
?? throw new Exception($"Was not able to deserialize package with {langName} language ({lFile.FullName})"); | ||
|
||
if (langCollection.TryGetValue(key, out string? resolved)) | ||
return resolved; | ||
} | ||
|
||
if (resolveDefault && lang != DefaultLanguage) | ||
return InternalResolveString(DefaultLanguage, key, resolveDefault, format); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using SKitLs.Utils.Localizations.Languages; | ||
using SKitLs.Utils.Localizations.Localizators; | ||
|
||
namespace SKitLs.Utils.Localizations.Model | ||
{ | ||
public abstract class LocalizatorBase : ILocalizator | ||
{ | ||
/// <inheritdoc/> | ||
public string NotDefinedKey { get; set; } = "local.KeyNotDefined"; | ||
|
||
/// <inheritdoc/> | ||
public LanguageCode DefaultLanguage { get; set; } | ||
|
||
/// <inheritdoc/> | ||
public string LocalsPath { get; private set; } | ||
|
||
public LocalizatorBase(string localsPath, LanguageCode defaultLanguage) | ||
{ | ||
LocalsPath = localsPath ?? throw new ArgumentNullException(nameof(localsPath)); | ||
DefaultLanguage = defaultLanguage; | ||
} | ||
|
||
public virtual string? ResolveString(LanguageCode? lang, string key, bool resolveDefault, params string?[] format) | ||
=> InternalResolveString(lang, key, resolveDefault, format); | ||
|
||
public virtual string ResolveStringOrFallback(LanguageCode? lang, string key, bool resolveDefault, params string?[] format) | ||
=> InternalResolveString(lang, key, resolveDefault, format) ?? FallbackString(lang, key, resolveDefault, format); | ||
|
||
protected abstract string? InternalResolveString(LanguageCode? lang, string key, bool resolveDefault, params string?[] format); | ||
|
||
protected virtual string FallbackString(LanguageCode? lang, string key, bool resolveDefault, params string?[] format) | ||
{ | ||
var reply = InternalResolveString(lang, NotDefinedKey, resolveDefault, Enum.GetName(lang ?? DefaultLanguage), key, LocalsPath) | ||
?? $"String Data is not defined ({key}:{Enum.GetName(lang ?? DefaultLanguage)}). Format params: {string.Join(", ", format)}"; | ||
return format.Length > 0 ? reply[..(reply.Length - 2)] + "." : reply + "None"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.