Skip to content

Commit

Permalink
Fix some titles being localized as "[GetCountry('MRY').Custom('get_mr…
Browse files Browse the repository at this point in the history
…y_adj')] Revolt" or "[GetCountry('PRY').Custom('get_pry_adj')] Revolt" (#2160)

![obraz](https://github.com/user-attachments/assets/f765f2f9-5f2a-4f4a-bbe0-2dd2cc0cbb6b)
  • Loading branch information
IhateTrains authored Sep 9, 2024
1 parent 7b3c259 commit 5a5abf1
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 7 deletions.
73 changes: 73 additions & 0 deletions ImperatorToCK3.UnitTests/Imperator/Countries/CountryNameTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using commonItems;
using commonItems.Localization;
using ImperatorToCK3.Imperator.Characters;
using ImperatorToCK3.Imperator.Countries;
using ImperatorToCK3.Imperator.Families;
using Xunit;

namespace ImperatorToCK3.UnitTests.Imperator.Countries;
Expand Down Expand Up @@ -129,4 +132,74 @@ public void GetNameLocBlockReturnsCorrectLocForRevolts() {
locBlock2["english"] = "Roman";
Assert.Equal("Roman Revolt", countryName.GetNameLocBlock(locDB, [])!["english"]);
}

[Fact]
public void DataTypesInCountryNamesAreReplaced() {
var reader = new BufferedReader(
"""
name="CIVILWAR_FACTION_NAME"
adjective="CIVILWAR_FACTION_ADJECTIVE"
base={
name="PRY_DYN"
adjective="PRY_DYN_ADJ"
}
"""
);

var countryName = ImperatorToCK3.Imperator.Countries.CountryName.Parse(reader);

var locDB = new LocDB("english");
var civilWarLocBlock = locDB.AddLocBlock("CIVILWAR_FACTION_NAME");
civilWarLocBlock["english"] = "$ADJ$ Revolt";
var pryAdjLocBlock = locDB.AddLocBlock("PRY_DYN_ADJ");
pryAdjLocBlock["english"] = "[GetCountry('PRY').Custom('get_pry_adj')]";
var antigonidPryAdjLocBlock = locDB.AddLocBlock("get_pry_adj_fetch"); // used when the PRY monarch family is Antigonid
antigonidPryAdjLocBlock["english"] = "Antigonid";
var fallbackPryAdjLocBlock = locDB.AddLocBlock("get_pry_adj_fallback"); // used when the PRY monarch family is not Antigonid
fallbackPryAdjLocBlock["english"] = "Phrygian";

Assert.Equal("Phrygian Revolt", countryName.GetNameLocBlock(locDB, [])!["english"]);
}

[Fact]
public void DataTypesInCountryAdjectivesAreReplaced() {
var reader = new BufferedReader(
"""
name="CIVILWAR_FACTION_NAME"
adjective="CIVILWAR_FACTION_ADJECTIVE"
base={
name="PRY_DYN"
adjective="PRY_DYN_ADJ"
}
"""
);
var countryName = ImperatorToCK3.Imperator.Countries.CountryName.Parse(reader);

var locDB = new LocDB("english");
var civilWarAdjLocBlock = locDB.AddLocBlock("CIVILWAR_FACTION_ADJECTIVE");
civilWarAdjLocBlock["english"] = "$ADJ$";
var pryAdjLocBlock = locDB.AddLocBlock("PRY_DYN_ADJ");
pryAdjLocBlock["english"] = "[GetCountry('PRY').Custom('get_pry_adj')]";
var antigonidPryAdjLocBlock = locDB.AddLocBlock("get_pry_adj_fetch");
antigonidPryAdjLocBlock["english"] = "Antigonid";
var fallbackPryAdjLocBlock = locDB.AddLocBlock("get_pry_adj_fallback");
fallbackPryAdjLocBlock["english"] = "Phrygian";

Assert.Equal("Phrygian", countryName.GetAdjectiveLocBlock(locDB, [])!["english"]);

// Check if get_pry_adj_fetch is used instead of get_pry_adj_fallback when the monarch family is Antigonid.
var families = new FamilyCollection();
families.LoadFamilies(new BufferedReader("1 = { key=\"Antigonid\" }"));

var characters = new CharacterCollection();
characters.LoadCharacters(new BufferedReader("1 = { family=1 country=1 }"));
characters.LinkFamilies(families);

var countries = new CountryCollection();
var phrygia = Country.Parse(new BufferedReader("{ tag=PRY monarch=1 }"), 1);
countries.Add(phrygia);
characters.LinkCountries(countries);

Assert.Equal("Antigonid", countryName.GetAdjectiveLocBlock(locDB, countries)!["english"]);
}
}
4 changes: 2 additions & 2 deletions ImperatorToCK3/CK3/Titles/LandedTitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,9 @@ private void SetDeJureKingdoms(CK3LocDB ck3LocDB, Date ck3BookmarkDate) {
);

var kingdomAdjLoc = ck3LocDB.GetOrCreateLocBlock(kingdom.Id + "_adj");
string duchyAdjLocKey = duchy.Id + "_adj"; // TODO: add some handling for the case where this is not localized
kingdomAdjLoc.ModifyForEveryLanguage(
(orig, language) => $"${duchy.Id}_adj$"
);
(orig, language) => $"${duchyAdjLocKey}$");

kingdom.DeJureLiege = capitalEmpireRealm;
duchy.DeJureLiege = kingdom;
Expand Down
1 change: 1 addition & 0 deletions ImperatorToCK3/CK3/Titles/Title.cs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ private void TrySetAdjectiveLoc(LocDB irLocDB, CountryCollection imperatorCountr
}
}
if (!adjSet) {
// Try to use the country name as adjective.
var adjLocalizationMatch = irLocDB.GetLocBlockForKey(ImperatorCountry.Tag);
if (adjLocalizationMatch is not null) {
var adjLocBlock = ck3LocDB.GetOrCreateLocBlock(locKey);
Expand Down
63 changes: 58 additions & 5 deletions ImperatorToCK3/Imperator/Countries/CountryName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ public object Clone() {
}
var locBlockToReturn = new LocBlock(Name, directNameLocMatch);
locBlockToReturn.ModifyForEveryLanguage(baseAdjLoc,
(orig, modifying, language) => orig?.Replace("$ADJ$", modifying)
);
(orig, modifying, language) => {
string? toReturn = orig?.Replace("$ADJ$", modifying);
if (toReturn is not null) {
toReturn = ReplaceDataTypes(toReturn, language, irLocDB, imperatorCountries);
}
return toReturn;
});
return locBlockToReturn;
}

Expand Down Expand Up @@ -79,9 +84,13 @@ private LocBlock GetCompositeNameLocBlock(string[] nameParts, LocDB irLocDB) {
var baseAdjLoc = BaseName?.GetAdjectiveLocBlock(irLocDB, imperatorCountries);
if (baseAdjLoc is not null) {
var locBlockToReturn = new LocBlock(adjKey, directAdjLocMatch);
locBlockToReturn.ModifyForEveryLanguage(baseAdjLoc, (orig, modifying, language) =>
orig?.Replace("$ADJ$", modifying)
);
locBlockToReturn.ModifyForEveryLanguage(baseAdjLoc, (orig, modifying, language) => {
var toReturn = orig?.Replace("$ADJ$", modifying);
if (toReturn is not null) {
toReturn = ReplaceDataTypes(toReturn, language, irLocDB, imperatorCountries);
}
return toReturn;
});
return locBlockToReturn;
}
} else if (directAdjLocMatch is not null) {
Expand Down Expand Up @@ -109,6 +118,50 @@ public string GetAdjectiveLocKey() {
}
return Name + "_ADJ";
}

private string ReplaceDataTypes(string loc, string language, LocDB irLocDB, CountryCollection irCountries) {
if (!loc.Contains("[GetCountry(")) {
return loc;
}

const string phrygianAdj = "[GetCountry('PRY').Custom('get_pry_adj')]";
Country? pry = irCountries.FirstOrDefault(country => country.Tag == "PRY");
LocBlock? pryAdjLocBlock;
if (pry is not null && pry.Monarch?.Family?.Key == "Antigonid") {
pryAdjLocBlock = irLocDB.GetLocBlockForKey("get_pry_adj_fetch");
} else {
pryAdjLocBlock = irLocDB.GetLocBlockForKey("get_pry_adj_fallback");
}
if (pryAdjLocBlock is not null && pryAdjLocBlock.HasLocForLanguage(language)) {
loc = loc.Replace(phrygianAdj, pryAdjLocBlock[language]);
}

const string mauryanAdj = "[GetCountry('MRY').Custom('get_mry_adj')]";
Country? mry = irCountries.FirstOrDefault(country => country.Tag == "MRY");
LocBlock? mryAdjLocBlock;
if (mry is not null && mry.Monarch?.Family?.Key == "Maurya") {
mryAdjLocBlock = irLocDB.GetLocBlockForKey("get_mry_adj_fetch");
} else {
mryAdjLocBlock = irLocDB.GetLocBlockForKey("get_mry_adj_fallback");
}
if (mryAdjLocBlock is not null && mryAdjLocBlock.HasLocForLanguage(language)) {
loc = loc.Replace(mauryanAdj, mryAdjLocBlock[language]);
}

const string seleucidAdj = "[GetCountry('SEL').Custom('get_sel_adj')]";
Country? sel = irCountries.FirstOrDefault(country => country.Tag == "SEL");
LocBlock? selAdjLocBlock;
if (sel is not null && sel.Monarch?.Family?.Key == "Seleukid") {
selAdjLocBlock = irLocDB.GetLocBlockForKey("get_sel_adj_fetch");
} else {
selAdjLocBlock = irLocDB.GetLocBlockForKey("get_sel_adj_fallback");
}
if (selAdjLocBlock is not null && selAdjLocBlock.HasLocForLanguage(language)) {
loc = loc.Replace(seleucidAdj, selAdjLocBlock[language]);
}

return loc;
}

public static CountryName Parse(BufferedReader reader) {
var countryName = new CountryName();
Expand Down

0 comments on commit 5a5abf1

Please sign in to comment.