2022-04-29 06:47:53 -04:00
|
|
|
// This project is licensed under the BSD 3-Clause license.
|
2020-12-13 07:48:15 -05:00
|
|
|
// See the LICENSE file in the project root for more information.
|
2022-04-29 06:47:53 -04:00
|
|
|
namespace CSTNet;
|
2020-12-13 07:48:15 -05:00
|
|
|
|
2022-10-10 02:54:46 -04:00
|
|
|
public class UIText : IUIText
|
2021-11-19 13:52:26 -05:00
|
|
|
{
|
2024-04-09 09:26:59 -04:00
|
|
|
/// <summary>
|
|
|
|
/// The language of the text.
|
|
|
|
/// </summary>
|
|
|
|
string Language { get; set; } = "english";
|
2022-07-21 10:23:31 -04:00
|
|
|
|
2024-04-09 09:47:13 -04:00
|
|
|
/// <summary>
|
|
|
|
/// The base directory for the language files.
|
|
|
|
/// </summary>
|
|
|
|
public string BasePath { get; set; } = Path.Combine(AppContext.BaseDirectory, "uitext");
|
2020-12-13 07:48:15 -05:00
|
|
|
|
2024-04-09 09:47:13 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Constructor for the UIText class.
|
|
|
|
/// </summary>
|
|
|
|
public UIText() { }
|
2020-12-13 07:48:15 -05:00
|
|
|
|
2024-04-09 09:47:13 -04:00
|
|
|
/// <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;
|
|
|
|
}
|
2020-12-13 07:48:15 -05:00
|
|
|
|
2024-04-09 09:47:13 -04:00
|
|
|
/// <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;
|
|
|
|
}
|
2020-12-13 07:48:15 -05:00
|
|
|
|
2024-04-09 09:47:13 -04:00
|
|
|
/// <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());
|
2020-12-13 07:48:15 -05:00
|
|
|
|
2024-04-09 09:47:13 -04:00
|
|
|
/// <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");
|
2020-12-13 07:48:15 -05:00
|
|
|
|
2024-04-09 09:47:13 -04:00
|
|
|
var files = Directory.GetFiles(langPath).Where(file =>
|
|
|
|
file.Contains(".cst") && Path.GetFileName(file).Split("_")[1] == id.ToString());
|
2023-01-07 10:45:54 -05:00
|
|
|
|
2024-04-09 09:47:13 -04:00
|
|
|
return files.Any() ? CST.Parse(File.ReadAllText(files.First()), key) : "***MISSING***";
|
|
|
|
}
|
2021-11-19 13:52:26 -05:00
|
|
|
|
2023-01-07 10:45:54 -05:00
|
|
|
|
2020-12-13 07:48:15 -05:00
|
|
|
}
|
2022-07-21 10:23:31 -04:00
|
|
|
|