// This project is licensed under the BSD 3-Clause license. // See the LICENSE file in the project root for more information. namespace CSTNet; public class UIText : IUIText { /// /// The language of the text. /// string Language { get; set; } = "english"; /// /// The base directory for the language files. /// public string[] BasePath { get; set; } = { AppContext.BaseDirectory, "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 and base directory. /// /// Language to load /// Base directory for the language files. public UIText(string language, params 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, 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"); // Get all the files in the language directory. var files = Directory.GetFiles(langPath); // 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; // 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.Substring(1, second - 1); // 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***"; } }