Skip to content

Commit

Permalink
Initial support for localizing region names
Browse files Browse the repository at this point in the history
Region names as read from MAPS.BSA are used in many places as a key and must be considered immutable. Only the region name intended for display on UIs and text output should use localized version of region names.
Added support for reading localized region names and updated where this is used for display in several places.
This probably isn't all-inclusive. There are no doubt other cases where internal region name is used for display that need to be migrated to use localized region name. I will migrate these as they become known.
Added check to strip duplicates from alphabet file when importing font and special characters.
Moved more string literals into Internal_Strings table.
  • Loading branch information
Interkarma committed Nov 24, 2022
1 parent 35f6978 commit dec7d3b
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 20 deletions.
Expand Up @@ -2596,7 +2596,7 @@ public static string Execute(params string[] args)
{
for (int region = 0; region < GameManager.Instance.PlayerEntity.RegionData.Length; region++)
{
string regionName = DaggerfallUnity.Instance.ContentReader.MapFileReader.GetRegionName(region);
string regionName = DaggerfallUnity.Instance.TextProvider.GetLocalizedRegionName(region);
string reputationString = string.Empty;
int rep = GameManager.Instance.PlayerEntity.RegionData[region].LegalRep;
if (rep > 80)
Expand Down Expand Up @@ -2652,7 +2652,7 @@ public static string Execute(params string[] args)
{
for (int region = 0; region < GameManager.Instance.PlayerEntity.RegionData.Length; region++)
{
string regionName = DaggerfallUnity.Instance.ContentReader.MapFileReader.GetRegionName(region);
string regionName = DaggerfallUnity.Instance.TextProvider.GetLocalizedRegionName(region);
int rep = GameManager.Instance.PlayerEntity.RegionData[region].LegalRep;
if (rep < 0)
{
Expand Down
Expand Up @@ -3695,6 +3695,18 @@ MonoBehaviour:
m_Key: regionNames
m_Metadata:
m_Items: []
- m_Id: 286315014360678400
m_Key: ocean
m_Metadata:
m_Items: []
- m_Id: 286315235807346688
m_Key: unknownUpper
m_Metadata:
m_Items: []
- m_Id: 286316113792614400
m_Key: switchToRegion
m_Metadata:
m_Items: []
m_Metadata:
m_Items: []
m_KeyGenerator:
Expand Down
16 changes: 14 additions & 2 deletions Assets/Localization/StringTables/Internal_Strings_en.asset
Expand Up @@ -4542,8 +4542,8 @@ MonoBehaviour:
Hammerfell sea coast
High
Rock bay coast
High Rock
bay coast
High Rock sea coast
Expand Down Expand Up @@ -4609,5 +4609,17 @@ MonoBehaviour:
Cybiades'
m_Metadata:
m_Items: []
- m_Id: 286315014360678400
m_Localized: Ocean
m_Metadata:
m_Items: []
- m_Id: 286315235807346688
m_Localized: Unknown
m_Metadata:
m_Items: []
- m_Id: 286316113792614400
m_Localized: 'Switch To: {0} Region'
m_Metadata:
m_Items: []
references:
version: 1
2 changes: 2 additions & 0 deletions Assets/Scripts/API/DFLocation.cs
Expand Up @@ -35,6 +35,8 @@ public struct DFLocation

/// <summary>
/// Name of the parent region.
/// IMPORTANT: This RegionName is used in many places as a string key and must always be the non-localized version as found in MAPS.BSA.
/// Anywhere using this for display to player should use TextProvider.GetLocalizedRegionName() instead.
/// </summary>
public string RegionName;

Expand Down
2 changes: 2 additions & 0 deletions Assets/Scripts/API/MapsFile.cs
Expand Up @@ -959,6 +959,8 @@ private bool ReadLocation(int region, int location, ref DFLocation dfLocation)
try
{
// Store parent region name
// IMPORTANT: This RegionName is used in many places as a string key and must always be the non-localized version as found in MAPS.BSA.
// Anywhere using this for display to player should use TextProvider.GetLocalizedRegionName() instead.
dfLocation.RegionName = RegionNames[region];

// Read MapPItem for this location
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Editor/AtlasEditorWindow.cs
Expand Up @@ -145,7 +145,7 @@ bool IsReady()

if (regionNames.Length == 0)
{
regionNames = (string[])MapsFile.RegionNames.Clone();
regionNames = (string[])MapsFile.RegionNames.Clone(); // Using non-localized name in atlas editor
System.Array.Sort(regionNames);
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Editor/SaveExplorerWindow.cs
Expand Up @@ -273,7 +273,7 @@ void DisplayFactionsFoldout()
string regionText = "None";
if (region > 0)
{
regionText = dfUnity.ContentReader.MapFileReader.GetRegionName(region);
regionText = dfUnity.ContentReader.MapFileReader.GetRegionName(region); // Using non-localized name in save explorer editor
}
EditorGUILayout.LabelField(string.Format("Region: {0}", regionText));
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Game/DaggerfallBankManager.cs
Expand Up @@ -432,7 +432,7 @@ public static void AllocateHouseToPlayer(BuildingSummary house, int regionIndex)

// Add note to journal
playerEntity.Notebook.AddNote(
TextManager.Instance.GetLocalizedText("houseDeed").Replace("%town", location.Name).Replace("%region", MapsFile.RegionNames[regionIndex]));
TextManager.Instance.GetLocalizedText("houseDeed").Replace("%town", location.Name).Replace("%region", DaggerfallUnity.Instance.TextProvider.GetLocalizedRegionName(regionIndex)));
}

public static TransactionResult SellHouse(int regionIndex)
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Game/LoanChecker.cs
Expand Up @@ -42,7 +42,7 @@ public static void CheckOverdueLoans(uint lastGameMinutes)
DaggerfallUI.AddHUDText(String.Format(TextManager.Instance.GetLocalizedText("loanReminder"),
DaggerfallBankManager.GetLoanedTotal(regionIndex)), loanReminderHUDDelay);
DaggerfallUI.AddHUDText(String.Format(TextManager.Instance.GetLocalizedText("loanReminder2"),
remainingMonths + 1, MapsFile.RegionNames[regionIndex]), loanReminderHUDDelay);
remainingMonths + 1, DaggerfallUnity.Instance.TextProvider.GetLocalizedRegionName(regionIndex)), loanReminderHUDDelay);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Game/NewLocationAlert.cs
@@ -1,4 +1,4 @@
// Project: Daggerfall Unity
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2022 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
Expand Down Expand Up @@ -76,7 +76,7 @@ void Update()
// Show text
if (LocationText)
{
string text = string.Format("{0}{1}({2})", playerGPS.CurrentLocation.Name, Environment.NewLine, playerGPS.CurrentLocation.RegionName);
string text = string.Format("{0}{1}({2})", playerGPS.CurrentLocation.Name, Environment.NewLine, DaggerfallUnity.Instance.TextProvider.GetLocalizedRegionName(playerGPS.CurrentLocation.RegionIndex));
ShowText(text);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Game/UserInterface/DaggerfallFont.cs
Expand Up @@ -716,7 +716,7 @@ void LoadCustomFontChars(string filename, TMP_FontAsset replacement)
List<uint> sourceUnicodes = new List<uint>(text.Length);
for (int c = 0; c < text.Length; c++)
{
if (text[c] >= 0x09 && text[c] <= 0x0d)
if (text[c] >= 0x09 && text[c] <= 0x0d || sourceUnicodes.Contains(text[c]))
continue; // Filter out codes 09-0d (tab through carriage return) so input txt can format a bit
else
sourceUnicodes.Add(text[c]);
Expand Down
Expand Up @@ -534,7 +534,7 @@ public static DaggerfallMessageBox CreateBankingStatusBox(IUserInterfaceWindow p
if (DaggerfallBankManager.GetAccountTotal(regionIndex) > 0 || DaggerfallBankManager.HasLoan(regionIndex))
{
TextFile.Formatting formatting = DaggerfallBankManager.HasDefaulted(regionIndex) ? TextFile.Formatting.TextHighlight : TextFile.Formatting.Text;
messages.AddRange(GetLoansLine(ShortenName(MapsFile.RegionNames[regionIndex], 12), DaggerfallBankManager.GetAccountTotal(regionIndex).ToString(), DaggerfallBankManager.GetLoanedTotal(regionIndex).ToString(), DaggerfallBankManager.GetLoanDueDateString(regionIndex), formatting));
messages.AddRange(GetLoansLine(ShortenName(DaggerfallUnity.Instance.TextProvider.GetLocalizedRegionName(regionIndex), 12), DaggerfallBankManager.GetAccountTotal(regionIndex).ToString(), DaggerfallBankManager.GetLoanedTotal(regionIndex).ToString(), DaggerfallBankManager.GetLoanDueDateString(regionIndex), formatting));
found = true;
}
}
Expand Down
Expand Up @@ -819,7 +819,7 @@ protected virtual void UpdateIdentifyTextureForPlayerRegion()
// Custom image must be based on 320x160 interior snip of TRAV0I00.IMG (so exclude top and bottom bars) but can be a higher resolution like 1600x800
Texture2D customRegionOverlayTexture;
if (importedOverlays.TryGetValue(playerRegion, out customRegionOverlayTexture) ||
TextureReplacement.TryImportImage(string.Format("{0}-{1}", overworldImgName, GetRegionName(playerRegion)), false, out customRegionOverlayTexture))
TextureReplacement.TryImportImage(string.Format("{0}-{1}", overworldImgName, GetRegionNameForMapReplacement(playerRegion)), false, out customRegionOverlayTexture))
{
identifyOverlayPanel.BackgroundTexture = importedOverlays[playerRegion] = customRegionOverlayTexture;
return;
Expand Down Expand Up @@ -1270,9 +1270,9 @@ protected virtual void UpdateRegionLabel()
if (RegionSelected == false)
regionLabel.Text = GetRegionName(mouseOverRegion);
else if (locationSelected)
regionLabel.Text = string.Format("{0} : {1}", DaggerfallUnity.ContentReader.MapFileReader.GetRegionName(mouseOverRegion), GetLocationNameInCurrentRegion(locationSummary.MapIndex, true));
regionLabel.Text = string.Format("{0} : {1}", GetRegionName(mouseOverRegion), GetLocationNameInCurrentRegion(locationSummary.MapIndex, true));
else if (MouseOverOtherRegion)
regionLabel.Text = string.Format("Switch To: {0} Region", DaggerfallUnity.ContentReader.MapFileReader.GetRegionName(mouseOverRegion));
regionLabel.Text = string.Format(TextManager.Instance.GetLocalizedText("switchToRegion"), GetRegionName(mouseOverRegion));
else
regionLabel.Text = GetRegionName(mouseOverRegion);
}
Expand Down Expand Up @@ -1572,7 +1572,11 @@ protected int GetPlayerRegion()
// Gets name of region
protected string GetRegionName(int region)
{
return DaggerfallUnity.Instance.ContentReader.MapFileReader.GetRegionName(region);
return DaggerfallUnity.Instance.TextProvider.GetLocalizedRegionName(region);
}
protected string GetRegionNameForMapReplacement(int region)
{
return DaggerfallUnity.Instance.ContentReader.MapFileReader.GetRegionName(region); // Using non-localized name for map replacement path
}

// Gets name of location in currently open region - tries world data replacement then falls back to MAPS.BSA
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Internal/PlayerGPS.cs
Expand Up @@ -552,11 +552,11 @@ private void UpdateWorldInfo(int x, int y)
currentPoliticIndex = dfUnity.ContentReader.MapFileReader.GetPoliticIndex(x, y);
climateSettings = MapsFile.GetWorldClimateSettings(currentClimateIndex);
if (currentPoliticIndex >= 128)
regionName = dfUnity.ContentReader.MapFileReader.GetRegionName(currentPoliticIndex - 128);
regionName = DaggerfallUnity.Instance.TextProvider.GetLocalizedRegionName(currentPoliticIndex - 128);
else if (currentPoliticIndex == 64)
regionName = "Ocean";
regionName = TextManager.Instance.GetLocalizedText("ocean");
else
regionName = "Unknown";
regionName = TextManager.Instance.GetLocalizedText("unknownUpper");

// Get region data
currentRegion = dfUnity.ContentReader.MapFileReader.GetRegion(CurrentRegionIndex);
Expand Down
11 changes: 11 additions & 0 deletions Assets/Scripts/Utility/FallbackTextProvider.cs
Expand Up @@ -201,6 +201,17 @@ public virtual string GetCustomEnemyName(int enemyId)
return fallback.GetCustomEnemyName(enemyId);
}

/// <summary>
/// Gets localized version of region name from string tables for display purposes only.
/// This is distinct from the name in MAPS.BSA which is also used internally as a key.
/// </summary>
/// <param name="regionIndex">Index of region.</param>
/// <returns>Localized name of region.</returns>
public virtual string GetLocalizedRegionName(int regionIndex)
{
return fallback.GetLocalizedRegionName(regionIndex);
}

/// <summary>
/// Attempts to read a localized string from a named table collection.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Utility/MacroHelper.cs
Expand Up @@ -1021,7 +1021,7 @@ public static string RegionInContext(IMacroContextProvider mcp)
{ // %reg
if (idRegion != -1)
{
return MapsFile.RegionNames[idRegion];
return DaggerfallUnity.Instance.TextProvider.GetLocalizedRegionName(idRegion);
}
else
return CurrentRegion(mcp);
Expand Down
25 changes: 25 additions & 0 deletions Assets/Scripts/Utility/TextProvider.cs
Expand Up @@ -133,6 +133,14 @@ public interface ITextProvider
/// <returns>Name if the enemy id is known, null otherwise</returns>
string GetCustomEnemyName(int enemyId);

/// <summary>
/// Gets localized version of region name from string tables for display purposes only.
/// This is distinct from the name in MAPS.BSA which is also used internally as a key.
/// </summary>
/// <param name="regionIndex">Index of region.</param>
/// <returns>Localized name of region.</returns>
string GetLocalizedRegionName(int regionIndex);

/// <summary>
/// Attempts to read a localized string from a named table collection.
/// </summary>
Expand Down Expand Up @@ -610,6 +618,23 @@ public string GetCustomEnemyName(int enemyId)
return null;
}

/// <summary>
/// Gets localized version of region name from string tables for display purposes only.
/// This is distinct from the name in MAPS.BSA which is also used internally as a key.
/// </summary>
/// <param name="regionIndex">Index of region.</param>
/// <returns>Localized name of region.</returns>
public string GetLocalizedRegionName(int regionIndex)
{
string[] regionNames = TextManager.Instance.GetLocalizedTextList("regionNames");
if (regionNames == null || regionNames.Length == 0 ||
regionIndex < 0 || regionIndex >= regionNames.Length)
{
return string.Empty; // Matching behaviour to MapsFile which returns empty string when out of range
}
return regionNames[regionIndex];
}

#region Protected Methods

protected void OpenTextRSCFile()
Expand Down

0 comments on commit dec7d3b

Please sign in to comment.