From 10a9958065b3415e58ea279e7c58820cc11e5c46 Mon Sep 17 00:00:00 2001 From: Tony Bark Date: Tue, 9 Apr 2024 09:47:13 -0400 Subject: [PATCH] Improved GetText function Glad I moved to the dev branch because I wasn't expecting this to be a breaking change. --- .github/workflows/build.yml | 1 - CSTNet/CSTNet.csproj | 2 +- CSTNet/IUIText.cs | 22 +++---- CSTNet/UIText.cs | 126 ++++++++++++++---------------------- notebooks/cst.ipynb | 2 + 5 files changed, 62 insertions(+), 91 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 40a286a..0ba0d26 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/CSTNet/CSTNet.csproj b/CSTNet/CSTNet.csproj index fdb390e..bca1405 100644 --- a/CSTNet/CSTNet.csproj +++ b/CSTNet/CSTNet.csproj @@ -2,7 +2,7 @@ net6.0;net8.0 - 2.1.101-alpha + 2.2.100-alpha enable latest enable diff --git a/CSTNet/IUIText.cs b/CSTNet/IUIText.cs index 74f535f..1a561ea 100644 --- a/CSTNet/IUIText.cs +++ b/CSTNet/IUIText.cs @@ -4,16 +4,16 @@ namespace CSTNet; public interface IUIText { - /// - /// The base directory for the language files. - /// - string[] BasePath { get; set; } + /// + /// The base directory for the language files. + /// + string BasePath { get; set; } - /// - /// Get the text for the given id and key. - /// - /// The id of the text. - /// The key of the text. - /// The text for the given id and key. - string GetText(int id, int key); + /// + /// Get the text for the given id and key. + /// + /// The id of the text. + /// The key of the text. + /// The text for the given id and key. + string GetText(int id, int key); } diff --git a/CSTNet/UIText.cs b/CSTNet/UIText.cs index 0d02d1a..3eedaa7 100644 --- a/CSTNet/UIText.cs +++ b/CSTNet/UIText.cs @@ -9,92 +9,62 @@ public class UIText : IUIText /// string Language { get; set; } = "english"; - /// - /// The base directory for the language files. - /// - public string[] BasePath { get; set; } = { AppContext.BaseDirectory, "uitext" }; + /// + /// The base directory for the language files. + /// + public string BasePath { get; set; } = Path.Combine(AppContext.BaseDirectory, "uitext"); - /// - /// Constructor for the UIText class. - /// - public UIText() { } + /// + /// Constructor for the UIText class. + /// + public UIText() { } - /// - /// Constructor for the UIText class. - /// Loads the language file for the specified language. - /// - /// Language to load - public UIText(string language) - { - Language = language; - } + /// + /// Constructor for the UIText class. + /// Loads the language file for the specified language. + /// + /// Language to load + public UIText(string language) + { + Language = language; + } - /// - /// Constructor for the UIText class. - /// Loads the language file for the specified language and base directory. - /// - /// Language to load - /// Base directory for the language files. - public UIText(string language, params string[] baseBath) - { - Language = language; - BasePath = baseBath; - } + /// + /// Constructor for the UIText class. + /// Loads the language file for the specified language and base directory. + /// + /// Language to load + /// Base directory for the language files. + public UIText(string language, string baseBath) + { + Language = language; + BasePath = baseBath; + } - /// - /// Get the text for the given id and key. - /// - /// The id of the text. - /// The key of the text. - /// The text for the given id and key. - public string GetText(int id, int key) => GetText(id, key.ToString()); + /// + /// Get the text for the given id and key. + /// + /// The id of the text. + /// The key of the text. + /// The text for the given id and key. + public string GetText(int id, int key) => GetText(id, key.ToString()); - /// - /// Get the text for the given id and key. - /// - /// The id of the text. - /// The key of the text. - /// The text for the given id and key. - 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"); + /// + /// Retrieves the text associated with the given id and key from the language files. + /// + /// The id to search for. + /// The key to parse from the content. + /// The parsed text if found, otherwise "***MISSING***". + 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***"; - } } diff --git a/notebooks/cst.ipynb b/notebooks/cst.ipynb index b141d45..62bdc57 100644 --- a/notebooks/cst.ipynb +++ b/notebooks/cst.ipynb @@ -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",