mirror of
https://github.com/tonytins/cstdotnet.git
synced 2025-07-04 21:50:27 -04:00
Switched back to "CST.NET" name
- Switched back to original CST.NET name because everyone was using that more. - Retroactively switched back to v1.1.100
This commit is contained in:
parent
dc62584b7c
commit
3de1fc5eca
13 changed files with 41 additions and 49 deletions
19
CSTNet/CSTNet.csproj
Normal file
19
CSTNet/CSTNet.csproj
Normal file
|
@ -0,0 +1,19 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Version>1.2.100</Version>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Authors>Tony Bark, Sixam Software</Authors>
|
||||
<PackageDescription>
|
||||
Caret-Separated Text (or CST) is a key-value pair format represented by digits or words as keys and the value as text enclosed between carets. ([key] ^[value]^)
|
||||
|
||||
Sixam.CST provides you the framework for parsing the CST format.
|
||||
</PackageDescription>
|
||||
<RepositoryUrl>https://github.com/sixamsoft/cst-dotnet</RepositoryUrl>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<RootNamespace>Sixam.CST</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
80
CSTNet/CaretSeparatedText.cs
Normal file
80
CSTNet/CaretSeparatedText.cs
Normal file
|
@ -0,0 +1,80 @@
|
|||
// 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 CaretSeparatedText
|
||||
{
|
||||
const char CARET = '^';
|
||||
const string LF = "\u000A";
|
||||
const string CR = "\u000D";
|
||||
const string CRLF = "\u000D\u000A";
|
||||
const string LS = "\u2028";
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value from the digit-based key.
|
||||
/// </summary>
|
||||
/// <returns>Returns the entry</returns>
|
||||
public static string Parse(string content, int key) => Parse(content, key.ToString());
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value from the string-based key.
|
||||
/// </summary>
|
||||
/// <returns>Returns the entry</returns>
|
||||
public static string Parse(string content, string key)
|
||||
{
|
||||
var entries = NormalizeEntries(content);
|
||||
return GetEntry(entries, key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the document's line endings with the native system line endings.
|
||||
/// </summary>
|
||||
/// <remarks>This stage ensures there are no crashes during parsing.</remarks>
|
||||
static IEnumerable<string> NormalizeEntries(string content)
|
||||
{
|
||||
if (!content.Contains(Environment.NewLine))
|
||||
{
|
||||
if (content.Contains(LF))
|
||||
content = content.Replace(LF, Environment.NewLine);
|
||||
|
||||
if (content.Contains(CR))
|
||||
content = content.Replace(CR, Environment.NewLine);
|
||||
|
||||
if (content.Contains(CRLF))
|
||||
content = content.Replace(CRLF, Environment.NewLine);
|
||||
|
||||
if (content.Contains(LS))
|
||||
content = content.Replace(LS, Environment.NewLine);
|
||||
}
|
||||
|
||||
var lines = content.Split(new[] { $"{CARET}{Environment.NewLine}" },
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
return lines.Where(line =>
|
||||
!line.StartsWith("//") &&
|
||||
!line.StartsWith("#") &&
|
||||
!line.StartsWith("/*") &&
|
||||
!line.EndsWith("*/"))
|
||||
.AsEnumerable();
|
||||
}
|
||||
|
||||
static string GetEntry(IEnumerable<string> entries, string key)
|
||||
{
|
||||
// Search through list
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
// If the line doesn't start with the key, keep searching.
|
||||
if (!entry.StartsWith(key))
|
||||
continue;
|
||||
|
||||
// Locate index, trim carets and return translation.
|
||||
var startIndex = entry.IndexOf(CARET);
|
||||
var line = entry.Substring(startIndex);
|
||||
|
||||
return line.TrimStart(CARET).TrimEnd(CARET);
|
||||
}
|
||||
|
||||
return "***MISSING***";
|
||||
}
|
||||
}
|
54
CSTNet/UIText.cs
Normal file
54
CSTNet/UIText.cs
Normal file
|
@ -0,0 +1,54 @@
|
|||
// 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
|
||||
{
|
||||
string Language { get; set; } = "english";
|
||||
public string[] BasePath { get; set; } = { AppContext.BaseDirectory, "uitext" };
|
||||
|
||||
public UIText() { }
|
||||
|
||||
public UIText(string language)
|
||||
{
|
||||
Language = language;
|
||||
}
|
||||
|
||||
public UIText(string language, params string[] baseBath)
|
||||
{
|
||||
Language = language;
|
||||
BasePath = baseBath;
|
||||
}
|
||||
|
||||
public string GetText(int id, int key) => GetText(id, key.ToString());
|
||||
|
||||
public string GetText(int id, string key)
|
||||
{
|
||||
var basePath = Path.Combine(BasePath);
|
||||
var langPath = Path.Combine(basePath, $"{Language}.dir");
|
||||
var files = Directory.GetFiles(langPath);
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
if (!file.Contains(".cst"))
|
||||
continue;
|
||||
|
||||
var ids = Path.GetFileName(file);
|
||||
var second = ids.IndexOf("_", 1, StringComparison.InvariantCultureIgnoreCase);
|
||||
|
||||
if (second == -1)
|
||||
continue;
|
||||
|
||||
ids = ids.Substring(1, second - 1);
|
||||
|
||||
if (ids != id.ToString())
|
||||
continue;
|
||||
|
||||
var content = File.ReadAllText(file);
|
||||
return CaretSeparatedText.Parse(content, key);
|
||||
}
|
||||
|
||||
return "***MISSING***";
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue