Skip to content

Commit f691a84

Browse files
authored
Use unlocalized name for name and adj before giving up (#353)
* Use unlocalized name for name and adj before giving up * Warnings * LocBlockCanBeConstructedWithOnlyEnglishLoc test
1 parent ab39e7c commit f691a84

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

ImperatorToCK3.UnitTests/Mappers/Localization/LocalizationMapperTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void LocalisationsCanBeLoadedAndMatched() {
3636
public void UnquotedLocIsIgnored() {
3737
var reader = new BufferedReader(
3838
"l_english:\n" +
39-
" key1:0 unqotedValue"
39+
" key1:0 unquotedValue"
4040
);
4141
var locMapper = new LocalizationMapper();
4242
locMapper.ScrapeStream(reader, "english");
@@ -148,5 +148,16 @@ public void LocBlockCanBeCopyConstructed() {
148148
Assert.Equal("e", copyLocBlock.simp_chinese);
149149
Assert.Equal("f", copyLocBlock.spanish);
150150
}
151+
152+
[Fact]
153+
public void LocBlockCanBeConstructedWithOnlyEnglishLoc() {
154+
var block = new LocBlock("Rule Britannia");
155+
const string expectedLoc = "Rule Britannia";
156+
Assert.Equal(expectedLoc, block.english);
157+
Assert.Equal(expectedLoc, block.german);
158+
Assert.Equal(expectedLoc, block.russian);
159+
Assert.Equal(expectedLoc, block.simp_chinese);
160+
Assert.Equal(expectedLoc, block.spanish);
161+
}
151162
}
152163
}

ImperatorToCK3/CK3/Titles/Title.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ public void InitializeFromTag(
184184
nameSet = true;
185185
}
186186
}
187+
if (!nameSet) {
188+
// use unlocalized name if not empty
189+
var name = ImperatorCountry.Name;
190+
if (!string.IsNullOrEmpty(name)) {
191+
Logger.Warn($"Using unlocalized Imperator name {name} as name for {Name}!");
192+
Localizations[Name] = new LocBlock(name);
193+
nameSet = true;
194+
}
195+
}
187196
// giving up
188197
if (!nameSet) {
189198
Logger.Warn($"{Name} needs help with localization! {ImperatorCountry.Name}?");
@@ -409,12 +418,8 @@ public void SetHolderId(string id, Date date) {
409418

410419
public List<RulerTerm> RulerTerms { get; private set; } = new();
411420
public int? DevelopmentLevel {
412-
get {
413-
return history.DevelopmentLevel;
414-
}
415-
set {
416-
history.DevelopmentLevel = value;
417-
}
421+
get => history.DevelopmentLevel;
422+
set => history.DevelopmentLevel = value;
418423
}
419424

420425
public Dictionary<string, LocBlock> Localizations { get; set; } = new();
@@ -429,15 +434,14 @@ private void TrySetAdjectiveLoc(LocalizationMapper localizationMapper, Dictionar
429434

430435
var adjSet = false;
431436

432-
if (ImperatorCountry.Tag == "PRY" || ImperatorCountry.Tag == "SEL" || ImperatorCountry.Tag == "MRY") { // these tags use customizable loc for adj
433-
LocBlock? validatedAdj = null;
434-
if (ImperatorCountry.Name == "PRY_DYN") {
435-
validatedAdj = localizationMapper.GetLocBlockForKey("get_pry_adj_fallback");
436-
} else if (ImperatorCountry.Name == "SEL_DYN") {
437-
validatedAdj = localizationMapper.GetLocBlockForKey("get_sel_adj_fallback");
438-
} else if (ImperatorCountry.Name == "MRY_DYN") {
439-
validatedAdj = localizationMapper.GetLocBlockForKey("get_mry_adj_fallback");
440-
}
437+
if (ImperatorCountry.Tag is "PRY" or "SEL" or "MRY") {
438+
// these tags use customizable loc for adj
439+
LocBlock? validatedAdj = ImperatorCountry.Name switch {
440+
"PRY_DYN" => localizationMapper.GetLocBlockForKey("get_pry_adj_fallback"),
441+
"SEL_DYN" => localizationMapper.GetLocBlockForKey("get_sel_adj_fallback"),
442+
"MRY_DYN" => localizationMapper.GetLocBlockForKey("get_mry_adj_fallback"),
443+
_ => null
444+
};
441445

442446
if (validatedAdj is not null) {
443447
Localizations[Name + "_adj"] = validatedAdj;
@@ -451,13 +455,22 @@ private void TrySetAdjectiveLoc(LocalizationMapper localizationMapper, Dictionar
451455
adjSet = true;
452456
}
453457
}
454-
if (!adjSet) { // final fallback
458+
if (!adjSet) {
455459
var adjLocalizationMatch = localizationMapper.GetLocBlockForKey(ImperatorCountry.Tag);
456460
if (adjLocalizationMatch is not null) {
457461
Localizations[Name + "_adj"] = adjLocalizationMatch;
458462
adjSet = true;
459463
}
460464
}
465+
if (!adjSet) {
466+
// use unlocalized name if not empty
467+
var name = ImperatorCountry.Name;
468+
if (!string.IsNullOrEmpty(name)) {
469+
Logger.Warn($"Using unlocalized Imperator name {name} as adjective for {Name}!");
470+
Localizations[Name + "_adj"] = new LocBlock(name);
471+
adjSet = true;
472+
}
473+
}
461474
// giving up
462475
if (!adjSet) {
463476
Logger.Warn($"{Name} needs help with localization for adjective! {ImperatorCountry.Name}_adj?");

ImperatorToCK3/Imperator/Countries/CountryName.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@ public object Clone() {
1919

2020
public LocBlock? GetNameLocBlock(LocalizationMapper localizationMapper, Dictionary<ulong, Country> imperatorCountries) {
2121
var directNameLocMatch = localizationMapper.GetLocBlockForKey(Name);
22-
if (directNameLocMatch is not null && Name == "CIVILWAR_FACTION_NAME") {
23-
// special case for revolts
24-
if (BaseName is not null) {
25-
var baseAdjLoc = BaseName.GetAdjectiveLocBlock(localizationMapper, imperatorCountries);
26-
if (baseAdjLoc is not null) {
27-
directNameLocMatch.ModifyForEveryLanguage(baseAdjLoc, (ref string orig, string modifying) =>
28-
orig = orig.Replace("$ADJ$", modifying)
29-
);
30-
return directNameLocMatch;
31-
}
32-
}
22+
if (directNameLocMatch is null || Name != "CIVILWAR_FACTION_NAME") {
23+
return directNameLocMatch;
24+
}
25+
26+
// special case for revolts
27+
if (BaseName is null) {
28+
return directNameLocMatch;
29+
}
30+
var baseAdjLoc = BaseName.GetAdjectiveLocBlock(localizationMapper, imperatorCountries);
31+
if (baseAdjLoc is null) {
32+
return directNameLocMatch;
3333
}
34+
directNameLocMatch.ModifyForEveryLanguage(baseAdjLoc, (ref string orig, string modifying) =>
35+
orig = orig.Replace("$ADJ$", modifying)
36+
);
3437
return directNameLocMatch;
3538
}
3639
public LocBlock? GetAdjectiveLocBlock(LocalizationMapper localizationMapper, Dictionary<ulong, Country> imperatorCountries) {

ImperatorToCK3/Mappers/Localization/LocBlock.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ public class LocBlock {
88
public string spanish = "";
99

1010
public LocBlock() { }
11+
12+
public LocBlock(string englishLoc) {
13+
english = englishLoc;
14+
FillMissingLocsWithEnglish();
15+
}
1116
public LocBlock(LocBlock otherLocBlock) {
1217
english = otherLocBlock.english;
1318
french = otherLocBlock.french;

0 commit comments

Comments
 (0)