mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-14 12:12:24 +01:00
fix: find a scenery's i18n tables whatever the case of their names
The i18n folder and its <scenery>_<language>.json tables are looked up through CaseInsensitivePaths, so on Linux a scenery's texts are translated when the table is named in other letter case than the scenery file.
This commit is contained in:
@@ -39,6 +39,24 @@ public class SceneryTranslationsTests
|
||||
Assert.Equal("Polski", scenery.LocalizedDescription(translations));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_tables_are_found_whatever_the_case_of_their_names()
|
||||
{
|
||||
var files = new InMemoryFileSystem(caseSensitive: true)
|
||||
.WithFile(TestInstallation.At("scenery", "Braniewo.scn"), "//$d @brief")
|
||||
.WithFile(Path.Combine(Path.GetFullPath(TestInstallation.At("scenery")), "I18N", "braniewo_en.json"),
|
||||
"""{ "brief": "English" }""")
|
||||
.WithFile(Path.Combine(Path.GetFullPath(TestInstallation.At("scenery")), "I18N", "BRANIEWO_PL.json"),
|
||||
"""{ "brief": "Polski" }""");
|
||||
var installation = new TestInstallation(files);
|
||||
var scenery = Assert.Single(installation.Sceneries.LoadAll());
|
||||
var translations = new SceneryTranslations(files, installation.Log);
|
||||
|
||||
translations.LoadFor(scenery, "Polski");
|
||||
|
||||
Assert.Equal("Polski", scenery.LocalizedDescription(translations));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Literal_text_and_unknown_keys_are_left_alone()
|
||||
{
|
||||
|
||||
@@ -14,12 +14,14 @@ public sealed class SceneryTranslations : ISceneryTranslations
|
||||
private const string BaseLanguage = "en";
|
||||
|
||||
private readonly IFileSystem _files;
|
||||
private readonly CaseInsensitivePaths _names;
|
||||
private readonly IDiagnosticsLog _log;
|
||||
private readonly Dictionary<string, string> _map = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public SceneryTranslations(IFileSystem files, IDiagnosticsLog log)
|
||||
{
|
||||
_files = files;
|
||||
_names = new CaseInsensitivePaths(files);
|
||||
_log = log;
|
||||
}
|
||||
|
||||
@@ -29,12 +31,11 @@ public sealed class SceneryTranslations : ISceneryTranslations
|
||||
|
||||
string sceneryDir = Path.GetDirectoryName(Path.GetFullPath(scenery.Path)) ?? "scenery";
|
||||
string id = Path.GetFileNameWithoutExtension(scenery.Path);
|
||||
string i18nDir = Path.Combine(sceneryDir, "i18n");
|
||||
string language = NormalizeLanguage(langCode);
|
||||
|
||||
TryLoad(Path.Combine(i18nDir, $"{id}_{BaseLanguage}.json"));
|
||||
TryLoad(_names.Find(sceneryDir, $"i18n/{id}_{BaseLanguage}.json"));
|
||||
if (!string.Equals(language, BaseLanguage, StringComparison.OrdinalIgnoreCase))
|
||||
TryLoad(Path.Combine(i18nDir, $"{id}_{language}.json"));
|
||||
TryLoad(_names.Find(sceneryDir, $"i18n/{id}_{language}.json"));
|
||||
}
|
||||
|
||||
public string Translate(string? text)
|
||||
@@ -50,9 +51,9 @@ public sealed class SceneryTranslations : ISceneryTranslations
|
||||
return _map.TryGetValue(key, out string? translated) ? translated : text;
|
||||
}
|
||||
|
||||
private void TryLoad(string path)
|
||||
private void TryLoad(string? path)
|
||||
{
|
||||
if (!_files.FileExists(path))
|
||||
if (path is null)
|
||||
return;
|
||||
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user