// 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; } = Path.Combine(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, 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());
///
/// 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");
var files = Directory.GetFiles(langPath).Where(file =>
file.Contains(".cst") && Path.GetFileName(file).Split("_")[1] == id.ToString());
return files.Any() ? CST.Parse(File.ReadAllText(files.First()), key) : "***MISSING***";
}
}