Implement markForPluralTranslation() which allows to translate plural-strings sent by the simulation.

Use it for the build-restriction notifications.

Based on patch by Safa Alfulaij, fixes #3081.

This was SVN commit r17398.
This commit is contained in:
elexis
2015-12-06 23:56:03 +00:00
parent 66bb4deef8
commit 9e39d6b46c
5 changed files with 62 additions and 19 deletions
+13 -2
View File
@@ -208,11 +208,22 @@ function translateObjectKeys(object, keys) {
* So it may only be used on a plain string,
* it won't have any effect on a calculated string.
*/
function markForTranslation(message) {
function markForTranslation(message)
{
return message;
}
function markForTranslationWithContext(context, message) {
function markForTranslationWithContext(context, message)
{
return message;
}
function markForPluralTranslation(singularMessage, pluralMessage, number)
{
return {
"message": singularMessage,
"pluralMessage": pluralMessage,
"pluralCount": number
};
}
@@ -130,7 +130,10 @@ function updateBuildingPlacementPreview()
{
var message = result.message;
if (result.translateMessage)
message = translate(message);
if (result.pluralMessage)
message = translatePlural(result.message, result.pluralMessage, result.pluralCount);
else
message = translate(message);
var parameters = result.parameters;
if (result.translateParameters)
translateObjectKeys(parameters, result.translateParameters);
+14 -7
View File
@@ -41,7 +41,8 @@
"translateWithContext": [[1], 2],
"translatePluralWithContext": [[1], 2, 3],
"markForTranslation": [1],
"markForTranslationWithContext": [[1], 2]
"markForTranslationWithContext": [[1], 2],
"markForPluralTranslation": [1, 2]
},
"commentTags": [
"Translation:"
@@ -85,7 +86,8 @@
"translateWithContext": [[1], 2],
"translatePluralWithContext": [[1], 2, 3],
"markForTranslation": [1],
"markForTranslationWithContext": [[1], 2]
"markForTranslationWithContext": [[1], 2],
"markForPluralTranslation": [1, 2]
},
"commentTags": [
"Translation:"
@@ -137,7 +139,8 @@
"translateWithContext": [[1], 2],
"translatePluralWithContext": [[1], 2, 3],
"markForTranslation": [1],
"markForTranslationWithContext": [[1], 2]
"markForTranslationWithContext": [[1], 2],
"markForPluralTranslation": [1, 2]
},
"commentTags": [
"Translation:"
@@ -188,7 +191,8 @@
"translateWithContext": [[1], 2],
"translatePluralWithContext": [[1], 2, 3],
"markForTranslation": [1],
"markForTranslationWithContext": [[1], 2]
"markForTranslationWithContext": [[1], 2],
"markForPluralTranslation": [1, 2]
},
"commentTags": [
"Translation:"
@@ -250,7 +254,8 @@
"translateWithContext": [[1], 2],
"translatePluralWithContext": [[1], 2, 3],
"markForTranslation": [1],
"markForTranslationWithContext": [[1], 2]
"markForTranslationWithContext": [[1], 2],
"markForPluralTranslation": [1, 2]
},
"commentTags": [
"Translation:"
@@ -446,7 +451,8 @@
"translateWithContext": [[1], 2],
"translatePluralWithContext": [[1], 2, 3],
"markForTranslation": [1],
"markForTranslationWithContext": [[1], 2]
"markForTranslationWithContext": [[1], 2],
"markForPluralTranslation": [1, 2]
},
"commentTags": [
"Translation:"
@@ -547,7 +553,8 @@
"format": "javascript-format",
"keywords": {
"markForTranslation": [1],
"markForTranslationWithContext": [[1], 2]
"markForTranslationWithContext": [[1], 2],
"markForPluralTranslation": [1, 2]
},
"commentTags": [
"Translation:"
@@ -69,6 +69,8 @@ BuildRestrictions.prototype.Init = function()
* "parameters": parameters to use in the GUI message
* "translateMessage": always true
* "translateParameters": list of parameters to translate
* "pluralMessage": we might return a plural translation instead (optional)
* "pluralCount": plural translation argument (optional)
* }
*
* Note: The entity which is used to check this should be a preview entity
@@ -257,11 +259,20 @@ BuildRestrictions.prototype.CheckPlacement = function()
var nearEnts = cmpRangeManager.ExecuteQuery(this.entity, 0, dist, [cmpPlayer.GetPlayerID()], IID_BuildRestrictions).filter(filter);
if (nearEnts.length)
{
result.message = markForTranslation("%(name)s too close to a %(category)s, must be at least %(distance)s meters away");
result.parameters.category = cat;
result.translateParameters.push("category");
result.parameters.distance = this.template.Distance.MinDistance;
return result; // Fail
var result = markForPluralTranslation(
"%(name)s too close to a %(category)s, must be at least 1 meter away",
"%(name)s too close to a %(category)s, must be at least %(distance)s meters away",
+this.template.Distance.MinDistance);
result.success = false;
result.translateMessage = true;
result.parameters = {
"name": name,
"category": cat,
"distance": this.template.Distance.MinDistance
};
result.translateParameters = ["name", "category"];
return result; // Fail
}
}
if (this.template.Distance.MaxDistance)
@@ -270,10 +281,19 @@ BuildRestrictions.prototype.CheckPlacement = function()
var nearEnts = cmpRangeManager.ExecuteQuery(this.entity, 0, dist, [cmpPlayer.GetPlayerID()], IID_BuildRestrictions).filter(filter);
if (!nearEnts.length)
{
result.message = markForTranslation("%(name)s too far from a %(category)s, must be within %(distance)s meters");
result.parameters.category = cat;
result.translateParameters.push("category");
result.parameters.distance = this.template.Distance.MaxDistance;
var result = markForPluralTranslation(
"%(name)s too far from a %(category)s, must be within 1 meter",
"%(name)s too far from a %(category)s, must be within %(distance)s meters",
+this.template.Distance.MinDistance);
result.success = false;
result.translateMessage = true;
result.parameters = {
"name": name,
"category": cat,
"distance": this.template.Distance.MaxDistance
};
result.translateParameters = ["name", "category"];
return result; // Fail
}
}
@@ -979,6 +979,8 @@ GuiInterface.prototype.DisplayRallyPoint = function(player, cmd)
* "parameters": parameters to use in the message
* "translateMessage": localisation info
* "translateParameters": localisation info
* "pluralMessage": we might return a plural translation instead (optional)
* "pluralCount": localisation info (optional)
* }
*/
GuiInterface.prototype.SetBuildingPlacementPreview = function(player, cmd)