More comfortable and safe resource name translation without breaking translation freeze.

Throw a reference error instead of silently using an untranslated string
if someone would have passed a non-existing translation context.
Throw a property undefined warning if someone passed a non-existing
resource when trying to translate it.
Both warnings had been introduced in 0f21151c4b, were removed by
52f311da2b but now occur without adding code explicitly.

Differential Revision: https://code.wildfiregames.com/D625
Reviewed By: s0600204
This was SVN commit r19771.
This commit is contained in:
elexis
2017-06-13 03:12:51 +00:00
parent ca9b3799b4
commit 9b041467a2
7 changed files with 26 additions and 25 deletions
+1 -6
View File
@@ -1,8 +1,3 @@
function getLocalizedResourceName(resourceName, context)
{
return translateWithContext(context, resourceName);
}
/**
* Format resource amounts to proper english and translate (for example: "200 food, 100 wood, and 300 metal").
*/
@@ -12,7 +7,7 @@ function getLocalizedResourceAmounts(resources)
.filter(type => !!resources[type])
.map(type => sprintf(translate("%(amount)s %(resourceType)s"), {
"amount": resources[type],
"resourceType": getLocalizedResourceName(g_ResourceData.GetResource(type).name, "withinSentence")
"resourceType": resourceNameWithinSentence(type)
}));
if (amounts.length > 1)
@@ -47,6 +47,16 @@ function resourceIcon(resource)
return '[icon="icon_' + resource + '"]';
}
function resourceNameFirstWord(type)
{
return translateWithContext("firstWord", g_ResourceData.GetNames()[type]);
}
function resourceNameWithinSentence(type)
{
return translateWithContext("withinSentence", g_ResourceData.GetNames()[type]);
}
function bodyFont(text)
{
return g_TooltipTextFormats.body[0] + text + g_TooltipTextFormats.body[1];
@@ -441,7 +441,6 @@ function diplomacyFormatStanceButtons(i, hidden)
function diplomacyFormatTributeButtons(i, hidden)
{
let resNames = g_ResourceData.GetNames();
let resCodes = g_ResourceData.GetCodes();
let r = 0;
for (let resCode of resCodes)
@@ -461,7 +460,7 @@ function diplomacyFormatTributeButtons(i, hidden)
continue;
button.enabled = controlsPlayer(g_ViewedPlayer);
button.tooltip = formatTributeTooltip(i, resNames[resCode], 100);
button.tooltip = formatTributeTooltip(i, resCode, 100);
button.onPress = (function(i, resCode, button) {
// Shift+click to send 500, shift+click+click to send 1000, etc.
// See INPUT_MASSTRIBUTING in input.js
@@ -479,14 +478,14 @@ function diplomacyFormatTributeButtons(i, hidden)
amounts[res] = 0;
amounts[resCode] = 100 * multiplier;
button.tooltip = formatTributeTooltip(i, resNames[resCode], amounts[resCode]);
button.tooltip = formatTributeTooltip(i, resCode, amounts[resCode]);
// This is in a closure so that we have access to `player`, `amounts`, and `multiplier` without some
// evil global variable hackery.
g_FlushTributing = function() {
Engine.PostNetworkCommand({ "type": "tribute", "player": i, "amounts": amounts });
multiplier = 1;
button.tooltip = formatTributeTooltip(i, resNames[resCode], 100);
button.tooltip = formatTributeTooltip(i, resCode, 100);
};
if (!isBatchTrainPressed)
@@ -713,7 +712,7 @@ function barterOpenCommon(resourceCode, idx, prefix)
for (let action of g_BarterActions)
barterButton[action] = Engine.GetGUIObjectByName(prefix + action + "Button[" + idx + "]");
let resource = getLocalizedResourceName(g_ResourceData.GetNames()[resourceCode], "withinSentence");
let resource = resourceNameWithinSentence(resourceCode);
barterButton.Buy.tooltip = sprintf(translate("Buy %(resource)s"), { "resource": resource });
barterButton.Sell.tooltip = sprintf(translate("Sell %(resource)s"), { "resource": resource });
@@ -1152,11 +1151,11 @@ function closeOpenDialogs()
closeObjectives();
}
function formatTributeTooltip(playerID, resource, amount)
function formatTributeTooltip(playerID, resourceCode, amount)
{
return sprintf(translate("Tribute %(resourceAmount)s %(resourceType)s to %(playerName)s. Shift-click to tribute %(greaterAmount)s."), {
"resourceAmount": amount,
"resourceType": getLocalizedResourceName(resource, "withinSentence"),
"resourceType": resourceNameWithinSentence(resourceCode),
"playerName": colorizePlayernameByID(playerID),
"greaterAmount": amount < 500 ? 500 : amount + 500
});
@@ -12,13 +12,10 @@ function layoutSelectionMultiple()
function getResourceTypeDisplayName(resourceType)
{
return getLocalizedResourceName(
g_ResourceData.GetNames()[
resourceType.generic == "treasure" ?
resourceType.specific :
resourceType.generic
],
"firstWord");
return resourceNameFirstWord(
resourceType.generic == "treasure" ?
resourceType.specific :
resourceType.generic);
}
// Updates the health bar of garrisoned units
@@ -1147,7 +1147,6 @@ function updatePlayerDisplay()
return;
let resCodes = g_ResourceData.GetCodes();
let resNames = g_ResourceData.GetNames();
for (let r = 0; r < resCodes.length; ++r)
{
let resourceObj = Engine.GetGUIObjectByName("resource[" + r + "]");
@@ -1157,7 +1156,7 @@ function updatePlayerDisplay()
let res = resCodes[r];
let tooltip = '[font="' + g_ResourceTitleFont + '"]' +
getLocalizedResourceName(resNames[res], "firstWord") + '[/font]';
resourceNameFirstWord(res) + '[/font]';
let descr = g_ResourceData.GetResource(res).description;
if (descr)
@@ -101,7 +101,7 @@ var g_ScorePanelsData = {
{ "identifier": "playername", "caption": translate("Player name"), "yStart": 26, "width": 200 },
...g_ResourceData.GetResources().map(res => ({
"identifier": res.code,
"caption": translateWithContext("firstWord", res.name),
"caption": resourceNameFirstWord(res.code),
"yStart": 34,
"width": 100
})),
@@ -153,8 +153,8 @@ var g_ScorePanelsData = {
"caption":
// Translation: use %(resourceWithinSentence)s if needed
sprintf(translate("%(resourceFirstWord)s exchanged"), {
"resourceFirstWord": translateWithContext("firstWord", res.name),
"resourceWithinSentence": translateWithContext("withinSentence", res.name)
"resourceFirstWord": resourceNameFirstWord(res.code),
"resourceWithinSentence": resourceNameWithinSentence(res.code)
}),
"yStart": 16,
"width": 100
@@ -6,6 +6,7 @@
<script file="gui/common/functions_utility.js"/>
<script file="gui/common/gamedescription.js"/>
<script file="gui/common/settings.js"/>
<script file="gui/common/tooltips.js"/>
<!-- After settings.js, which defines g_Settings and g_MaxPlayers. -->
<script file="gui/summary/counters.js"/>