Skip to content

Commit

Permalink
Allow translation of quest messages from CSV
Browse files Browse the repository at this point in the history
Translator can provide an Internal_Quests CSV file with translations of one or more quest messages.
This process is simple to use and decouples translation of message from quest script.
  • Loading branch information
Interkarma committed Nov 22, 2022
1 parent 899e4d6 commit a771d33
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
13 changes: 12 additions & 1 deletion Assets/Scripts/Game/Questing/Message.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 @@ -138,6 +138,17 @@ public void LoadMessage(int id, string[] source)
variants.Add(variant);
}

/// <summary>
/// Replace a message with new source lines (e.g. from translation data).
/// </summary>
/// <param name="id">ID of message.</param>
/// <param name="source">Array of source lines in message.</param>
public void ReplaceMessage(int id, string[] source)
{
variants.Clear();
LoadMessage(id, source);
}

/// <summary>
/// Gets text tokens for this message.
/// Will return a random variant if variant = -1 and more than one variant exists.
Expand Down
9 changes: 6 additions & 3 deletions Assets/Scripts/Game/Questing/Quest.cs
Expand Up @@ -18,6 +18,7 @@
using DaggerfallWorkshop.Game.Items;
using DaggerfallWorkshop.Game.UserInterfaceWindows;
using FullSerializer;
using DaggerfallWorkshop.Localization;

namespace DaggerfallWorkshop.Game.Questing
{
Expand Down Expand Up @@ -672,11 +673,13 @@ public Message GetMessage(int messageID)
else
return null;

// TODO: Attempt to get localized text for message
// Attempt to get localized text for message
string localizedString;
string key = string.Format("{0}.{1}", QuestName, messageID.ToString());
string localizedText = TextManager.Instance.GetLocalizedText(key, TextCollections.TextQuests);
if (!string.IsNullOrEmpty(localizedText))
if (TextManager.Instance.TryGetLocalizedText(TextCollections.TextQuests, key, out localizedString))
{
string[] lines = localizedString.Split('\n');
result.ReplaceMessage(messageID, lines);
}

return result;
Expand Down
13 changes: 13 additions & 0 deletions Assets/Scripts/Game/TextManager.cs
Expand Up @@ -387,6 +387,19 @@ public string GetLocalizedEnemyName(int enemyID)
}
}

/// <summary>
/// Tries to gets text value from localization in TextProvider.
/// Will use current locale if available in collection.
/// </summary>
/// <param name="collection">Enum value to lookup collection name in TextManager.</param>
/// <param name="key">Key of text in table.</param>
/// <param name="localizedString">Result of lookup if found.</param>
/// <returns>True if text found, otherwise false.</returns>
public bool TryGetLocalizedText(TextCollections collection, string key, out string localizedString)
{
return TryGetLocalizedText(GetRuntimeCollectionName(collection), key, out localizedString);
}

#endregion

#region Private Localized Text Methods
Expand Down

0 comments on commit a771d33

Please sign in to comment.