Improved GetText function

Glad I moved to the dev branch because I wasn't expecting this to be a breaking change.
This commit is contained in:
Tony Bark 2024-04-09 09:47:13 -04:00
parent 488c45620f
commit 10a9958065
5 changed files with 62 additions and 91 deletions

View file

@ -42,7 +42,6 @@ jobs:
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ matrix.dotnet }}
dotnet-quality: 'preview'
- name: Install dependencies
run: dotnet restore
- name: Test

View file

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<Version>2.1.101-alpha</Version>
<Version>2.2.100-alpha</Version>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>

View file

@ -4,16 +4,16 @@ namespace CSTNet;
public interface IUIText
{
/// <summary>
/// The base directory for the language files.
/// </summary>
string[] BasePath { get; set; }
/// <summary>
/// The base directory for the language files.
/// </summary>
string BasePath { get; set; }
/// <summary>
/// Get the text for the given id and key.
/// </summary>
/// <param name="id">The id of the text.</param>
/// <param name="key">The key of the text.</param>
/// <returns>The text for the given id and key.</returns>
string GetText(int id, int key);
/// <summary>
/// Get the text for the given id and key.
/// </summary>
/// <param name="id">The id of the text.</param>
/// <param name="key">The key of the text.</param>
/// <returns>The text for the given id and key.</returns>
string GetText(int id, int key);
}

View file

@ -9,92 +9,62 @@ public class UIText : IUIText
/// </summary>
string Language { get; set; } = "english";
/// <summary>
/// The base directory for the language files.
/// </summary>
public string[] BasePath { get; set; } = { AppContext.BaseDirectory, "uitext" };
/// <summary>
/// The base directory for the language files.
/// </summary>
public string BasePath { get; set; } = Path.Combine(AppContext.BaseDirectory, "uitext");
/// <summary>
/// Constructor for the UIText class.
/// </summary>
public UIText() { }
/// <summary>
/// Constructor for the UIText class.
/// </summary>
public UIText() { }
/// <summary>
/// Constructor for the UIText class.
/// Loads the language file for the specified language.
/// </summary>
/// <param name="language">Language to load</param>
public UIText(string language)
{
Language = language;
}
/// <summary>
/// Constructor for the UIText class.
/// Loads the language file for the specified language.
/// </summary>
/// <param name="language">Language to load</param>
public UIText(string language)
{
Language = language;
}
/// <summary>
/// Constructor for the UIText class.
/// Loads the language file for the specified language and base directory.
/// </summary>
/// <param name="language">Language to load</param>
/// <param name="baseBath">Base directory for the language files.</param>
public UIText(string language, params string[] baseBath)
{
Language = language;
BasePath = baseBath;
}
/// <summary>
/// Constructor for the UIText class.
/// Loads the language file for the specified language and base directory.
/// </summary>
/// <param name="language">Language to load</param>
/// <param name="baseBath">Base directory for the language files.</param>
public UIText(string language, string baseBath)
{
Language = language;
BasePath = baseBath;
}
/// <summary>
/// Get the text for the given id and key.
/// </summary>
/// <param name="id">The id of the text.</param>
/// <param name="key">The key of the text.</param>
/// <returns>The text for the given id and key.</returns>
public string GetText(int id, int key) => GetText(id, key.ToString());
/// <summary>
/// Get the text for the given id and key.
/// </summary>
/// <param name="id">The id of the text.</param>
/// <param name="key">The key of the text.</param>
/// <returns>The text for the given id and key.</returns>
public string GetText(int id, int key) => GetText(id, key.ToString());
/// <summary>
/// Get the text for the given id and key.
/// </summary>
/// <param name="id">The id of the text.</param>
/// <param name="key">The key of the text.</param>
/// <returns>The text for the given id and key.</returns>
public string GetText(int id, string key)
{
// Combine the base path and language path to get the full path of the language file.
var basePath = Path.Combine(BasePath);
var langPath = Path.Combine(basePath, $"{Language}.dir");
/// <summary>
/// Retrieves the text associated with the given id and key from the language files.
/// </summary>
/// <param name="id">The id to search for.</param>
/// <param name="key">The key to parse from the content.</param>
/// <returns>The parsed text if found, otherwise "***MISSING***".</returns>
public string GetText(int id, string key)
{
var langPath = Path.Combine(BasePath, $"{Language}.dir");
// Get all the files in the language directory.
var files = Directory.GetFiles(langPath);
var files = Directory.GetFiles(langPath).Where(file =>
file.Contains(".cst") && Path.GetFileName(file).Split("_")[1] == id.ToString());
// Iterate through the files in the language directory.
foreach (var file in files)
{
// Skip files that do not have the ".cst" extension.
if (!file.Contains(".cst"))
continue;
return files.Any() ? CST.Parse(File.ReadAllText(files.First()), key) : "***MISSING***";
}
// Get the id of the current file.
var ids = Path.GetFileName(file);
var second = ids.IndexOf("_", 1, StringComparison.InvariantCultureIgnoreCase);
if (second == -1)
continue;
ids = ids[1..second];
// If the id of the current file does not match the id passed to the function,
// skip to the next file.
if (ids != id.ToString())
continue;
// Read the content of the current file.
var content = File.ReadAllText(file);
// Return the text for the specified key.
return CST.Parse(content, key);
}
// If no text is found, return a default string.
return "***MISSING***";
}
}

View file

@ -6,6 +6,8 @@
"source": [
"# Caret-Separated Text\n",
"\n",
"> The notebooks that started it all!\n",
"\n",
"Caret-Separated Text (or CST) is a key-value pair format represented by numbers as keys and the value is the string enclosed between carets (^) that contains the translation. Any text which is not enclosed with carets is considered a comment and ignored.\n",
"\n",
"## CST.NET\n",