Skip to content

Commit

Permalink
Merge pull request #74 from hmlendea/default-names
Browse files Browse the repository at this point in the history
Localise default names
  • Loading branch information
hmlendea authored Jun 6, 2024
2 parents 11b1f96 + 21dcd95 commit 70d746e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ public GameIdEntity(string game, string gameLocationId)
Game = game;
Id = gameLocationId;
}

[XmlAttribute("game")]
public string Game { get; set; }

[XmlAttribute("type")]
public string Type { get; set; }

[XmlAttribute("parent")]
public string Parent { get; set; }

[XmlAttribute("defaultLanguage")]
public string DefaultNameLanguageId { get; set; }

[XmlText]
public string Id { get; set; }
}
Expand Down
2 changes: 2 additions & 0 deletions MoreCulturalNamesBuilder/Service/Mapping/GameIdMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal static GameId ToServiceModel(this GameIdEntity dataObject)
serviceModel.Game = dataObject.Game;
serviceModel.Type = dataObject.Type;
serviceModel.Parent = dataObject.Parent;
serviceModel.DefaultNameLanguageId = dataObject.DefaultNameLanguageId;
serviceModel.Id = dataObject.Id;

return serviceModel;
Expand All @@ -25,6 +26,7 @@ internal static GameIdEntity ToDataObject(this GameId serviceModel)
dataObject.Game = serviceModel.Game;
dataObject.Type = serviceModel.Type;
dataObject.Parent = serviceModel.Parent;
dataObject.DefaultNameLanguageId = serviceModel.DefaultNameLanguageId;
dataObject.Id = serviceModel.Id;

return dataObject;
Expand Down
21 changes: 18 additions & 3 deletions MoreCulturalNamesBuilder/Service/ModBuilders/CK2ModBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,32 @@ string GenerateLocalisationFileContent()
{
foreach (Localisation localisation in localisations[locationGameId])
{
GameId gameId = locationGameIds.First(x => x.Id.Equals(locationGameId));
if (localisation.LanguageId.Equals(gameId.DefaultNameLanguageId))
{
string normalisedName = nameNormaliser.ToWindows1252(localisation.Name);
lines.Add(
$"{locationGameId}" +
$";{normalisedName};{normalisedName};{normalisedName};;{normalisedName};;;;;;;;;x");
}
if (string.IsNullOrWhiteSpace(localisation.Adjective))
{
continue;
}
string normalisedAdjective = nameNormaliser.ToWindows1252(localisation.Adjective);
string line =
lines.Add(
$"{locationGameId}_adj_{localisation.LanguageGameId}" +
$";{normalisedAdjective};{normalisedAdjective};{normalisedAdjective};;{normalisedAdjective};;;;;;;;;x";
$";{normalisedAdjective};{normalisedAdjective};{normalisedAdjective};;{normalisedAdjective};;;;;;;;;x");
lines.Add(line);
if (localisation.LanguageId.Equals(gameId.DefaultNameLanguageId))
{
lines.Add(
$"{locationGameId}_adj" +
$";{normalisedAdjective};{normalisedAdjective};{normalisedAdjective};;{normalisedAdjective};;;;;;;;;x");
}
};
});

Expand Down
70 changes: 62 additions & 8 deletions MoreCulturalNamesBuilder/Service/ModBuilders/CK3ModBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,22 @@ protected override string GetTitleLocalisationsContent(string line, string gameI

protected override void CreateLocalisationFiles(string localisationDirectoryPath)
{
string content = GenerateLocalisationFileContent();

Parallel.ForEach(
new List<string>{ "english", "french", "german", "spanish" },
fileLanguage => CreateLocalisationFile(localisationDirectoryPath, fileLanguage, content));
string defaultLocalisationsFileContent = GenerateDefaultNamesLocalisationFileContent();
string dynamicLocalisationsFileContent = GenerateDynamicNamesLocalisationFileContent();

List<string> localisationLanguages = new List<string>{ "english", "french", "german", "spanish" };

Parallel.ForEach(localisationLanguages, fileLanguage => CreateLocalisationFile(
localisationDirectoryPath,
"titles",
fileLanguage,
defaultLocalisationsFileContent));

Parallel.ForEach(localisationLanguages, fileLanguage => CreateLocalisationFile(
localisationDirectoryPath,
"titles_cultural_names",
fileLanguage,
dynamicLocalisationsFileContent));
}

protected override void CreateDescriptorFiles()
Expand All @@ -145,7 +156,48 @@ protected override void CreateDescriptorFiles()
File.WriteAllText(innerDescriptorFilePath, innerDescriptorContent);
}

string GenerateLocalisationFileContent()
string GenerateDefaultNamesLocalisationFileContent()
{
ConcurrentBag<string> lines = new ConcurrentBag<string>();

Parallel.ForEach(locations.Values, location =>
{
foreach (GameId gameId in location.GameIds.Where(x => x.Game.Equals(Settings.Mod.Game)))
{
if (string.IsNullOrWhiteSpace(gameId.DefaultNameLanguageId))
{
continue;
}
Localisation defaultLocalisation = localisations[gameId.Id]
.FirstOrDefault(x => x.LanguageId.Equals(gameId.DefaultNameLanguageId));
if (defaultLocalisation is null)
{
continue;
}
string normalisedName = nameNormaliser.ToCK3Charset(defaultLocalisation.Name);
lines.Add(
$"{defaultLocalisation.GameId}" +
$";{normalisedName};{normalisedName};{normalisedName};;{normalisedName};;;;;;;;;x");
if (!string.IsNullOrWhiteSpace(defaultLocalisation.Adjective))
{
string normalisedAdjective = nameNormaliser.ToCK3Charset(defaultLocalisation.Name);
lines.Add(
$"{defaultLocalisation.GameId}" +
$";{normalisedAdjective};{normalisedAdjective};{normalisedAdjective};;{normalisedAdjective};;;;;;;;;x");
}
}
});

return string.Join(
Environment.NewLine,
lines.OrderBy(line => line));
}

string GenerateDynamicNamesLocalisationFileContent()
{
ConcurrentBag<string> lines = new ConcurrentBag<string>();

Expand All @@ -172,6 +224,8 @@ string GenerateLocalisationFileContent()
lines.Add(titleLocalisationDefinition);
GameId gameId = locationGameIds.First(x => x.Id.Equals(localisation.GameId));
if (!string.IsNullOrWhiteSpace(localisation.Adjective))
{
string normalisedAdjective = nameNormaliser.ToCK3Charset(localisation.Adjective);
Expand All @@ -184,10 +238,10 @@ string GenerateLocalisationFileContent()
lines.OrderBy(line => line));
}

void CreateLocalisationFile(string localisationDirectoryPath, string language, string content)
void CreateLocalisationFile(string localisationDirectoryPath, string fileLabel, string language, string content)
{
string fileContent = $"l_{language}:{Environment.NewLine}{content}";
string fileName = $"{Settings.Mod.Id}_titles_cultural_names_l_{language}.yml";
string fileName = $"{Settings.Mod.Id}_{fileLabel}_l_{language}.yml";
string filePath = Path.Combine(localisationDirectoryPath, fileName);

File.WriteAllText(filePath, fileContent, Encoding.UTF8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,34 @@ void CreateDescriptorFiles()

string GenerateLocalisationFileContent()
{
ConcurrentBag<string> lines = new ConcurrentBag<string>();
ConcurrentBag<string> lines = new();

Parallel.ForEach(localisations.Keys.OrderBy(x => int.Parse(x)), provinceId =>
Parallel.ForEach(localisations.Keys, provinceId =>
{
foreach (string culture in localisations[provinceId].Keys.OrderBy(x => x))
GameId gameId = locationGameIds.First(x => x.Id.Equals(provinceId));
IDictionary<string, Localisation> provinceLocalisations = localisations[provinceId];
Localisation defaultLocalisation = provinceLocalisations.Values
.FirstOrDefault(x => x.LanguageId.Equals(gameId.DefaultNameLanguageId));
if (defaultLocalisation is not null)
{
Localisation localisation = localisations[provinceId][culture];
string provinceDefaultLocalisationDefinition = GenerateLocationLocalisationLine(
defaultLocalisation,
$"PROV{provinceId}");
string provinceLocalisationDefinition =
$" PROV{provinceId}_{localisation.LanguageGameId}:0 " +
$"\"{nameNormaliser.ToImperatorRomeCharset(localisation.Name)}\"";
lines.Add(provinceDefaultLocalisationDefinition);
}
if (Settings.Output.AreVerboseCommentsEnabled)
{
provinceLocalisationDefinition += $" # Language={localisation.LanguageId}";
}
foreach (string culture in provinceLocalisations.Keys.OrderBy(x => x))
{
Localisation localisation = provinceLocalisations[culture];
if (!string.IsNullOrWhiteSpace(localisation.Comment))
{
provinceLocalisationDefinition += $" # {localisation.Comment}";
}
string provinceCulturalLocalisationDefinition = GenerateLocationLocalisationLine(
localisation,
$"PROV{provinceId}_{localisation.LanguageGameId}");
lines.Add(provinceLocalisationDefinition);
lines.Add(provinceCulturalLocalisationDefinition);
}
});

Expand All @@ -168,6 +173,25 @@ string GenerateLocalisationFileContent()
lines.OrderBy(line => line));
}

string GenerateLocationLocalisationLine(Localisation localisation, string localisationKey)
{
string provinceLocalisationDefinition =
$" {localisationKey}:0 " +
$"\"{nameNormaliser.ToImperatorRomeCharset(localisation.Name)}\"";

if (Settings.Output.AreVerboseCommentsEnabled)
{
provinceLocalisationDefinition += $" # Language={localisation.LanguageId}";
}

if (!string.IsNullOrWhiteSpace(localisation.Comment))
{
provinceLocalisationDefinition += $" # {localisation.Comment}";
}

return provinceLocalisationDefinition;
}

string GenerateMainDescriptorContent()
=> GenerateInnerDescriptorContent() +
Environment.NewLine +
Expand Down
4 changes: 2 additions & 2 deletions MoreCulturalNamesBuilder/Service/ModBuilders/ModBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public abstract class ModBuilder : IModBuilder

protected readonly Settings Settings;

protected IDictionary<string, Location> locations;
protected IDictionary<string, Language> languages;
protected IDictionary<string, Location> locations;

protected IEnumerable<GameId> locationGameIds;
protected IEnumerable<GameId> languageGameIds;
protected IEnumerable<GameId> locationGameIds;
protected IEnumerable<GameId> titleGameIds;

public ModBuilder(
Expand Down
2 changes: 2 additions & 0 deletions MoreCulturalNamesBuilder/Service/Models/GameId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public sealed class GameId

public string Parent { get; set; }

public string DefaultNameLanguageId { get; set; }

public string Id { get; set; }
}
}

0 comments on commit 70d746e

Please sign in to comment.