Text key { } codes

Thalassicus

Bytes and Nibblers
Joined
Nov 9, 2005
Messages
11,057
Location
Texas
Does anyone know if the {} control codes for text keys follow a pattern from a specific programming or data language? I've found a few things like the ones below, and I'm curious what else is possible.

{TXT_KEY_RETIRE : upper}
{TXT_KEY_GRAMMAR_A_AN << {@2_Adjective}}
{1_Turns:number}
{1_Turns: plural 1?Turn; other?Turns;}
{@2_BldgName: gender *:an?an; other?a;}

It's interesting how they used the gender-detecting method from international languages as a clever way to do a/an for English. I'm also rather curious about the :number code. I wonder why they use that for a few numbers, but not others?
 
I know that it allow some basic formatting for number, like this:

{1_Num:number "#.##"}

There is also this that I think can be used in the <Text> tag:

<Text>some text. {TXT_KEY_XXX}</Text>
 
All used variations for number formatting are

Code:
{1_Gold}
{1_Gold: number}
{1_Gold: number #}
{1_Gold: number #.##}
{1_Gold: number "#.##"}
{1_Gold: number "'(+'##')';'[COLOR:255:60:60:255](-'##')'"}

where "_Gold" is optional (this is just a cue to translators so they know what to expect in the placeholder)
 
and I'm curious what else is possible.

Every {...} used in the I18N text key and UI files is matched by one of the following

Code:
TXTKEY  ::= \{TXT_KEY_[a-z0-9_]*( *: *(upper|lower))?\}
NUMBER  ::= \{@?[0-9]+[a-z0-9_]* *: *(number|spellout)( *"?#*(\.#*)?"?)?\}
FORMAT  ::= \{@?[0-9]+[a-z0-9_]*( *: *(textKey|upper|lower))?(\[[0-9]+\])?\}
GRAMMER ::=\{TXT_KEY_GRAMMAR_(UPPER_)?A_AN &lt;&lt; *\[FORMAT\]\}

PLURAL  ::= \{@?[0-9]+[a-z0-9_]* *: *plural( *([123]|other)\?[^;}]*[;}]?)*\}
GENDER  ::= \{@?[0-9]+[a-z0-9_]* *: *gender( *(([mfn][^?]*)|other|(\*:(an|vowel|no_article|sinartmasc|sinartfem)))\?[^;]*;)+\}

(except for what appears to be a typo in one of the files "*no_article" instead of "*:no_article" and the unique positive/negative number formatting)

The text that appears within a replacment string with the PLURAL and GENDER formats may itself be formatted, eg

Code:
{2: plural 2?{1: gender masculine?Los {2_CivName} ya habían; feminine?Las {2_CivName} ya habían;}; other?{2_CivName} ya había;}
 
I wonder what the difference between

{@2_CivShortDesc}

and

{2_CivShortDesc}

is?

And what the relevance of [3] is in

@2_CityStateTraitAdj[3]

Answer to own question, it relates to TXT_KEY_ entries such as

Code:
<Row Tag="TXT_KEY_CITY_STATE_MERCANTILE_ADJECTIVE">
  <Text>mercantilista|mercantilista|mercantilistas|mercantilistas</Text>
  <Gender>masculine|feminine|masculine|feminine</Gender>
  <Plurality>1|1|2|2</Plurality>
</Row>
 
Outside of translation, about the most useful thing I can think of for the gender/plural options are

Code:
iGoats = 1; print(Locale.Lookup("There {1: plural 1?is; 2?are;} {1} goat{1: plural 2?s;}", iGoats))
 --> There is 1 goat
iGoats = 2; print(Locale.Lookup("There {1: plural 1?is; 2?are;} {1} goat{1: plural 2?s;}", iGoats))
 --> There are 2 goats
iGoats = 3; print(Locale.Lookup("There {1: plural 1?is; 2?are;} {1} goat{1: plural 2?s;}", iGoats))
 --> There are 3 goats
iGoats = 0; print(Locale.Lookup("There {1: plural 1?is; 2?are;} {1} goat{1: plural 2?s;}", iGoats))
 --> There are 0 goats

Code:
sUnit="UNIT_ARCHER"; print(Locale.Lookup("You have been attacked by {1: gender *:an?an; other?a;} {1}", GameInfo.Units[sUnit].Description))
 --> You have been attacked by an Archer
sUnit="UNIT_WARRIOR"; print(Locale.Lookup("You have been attacked by {1: gender *:an?an; other?a;} {1}", GameInfo.Units[sUnit].Description))
 --> You have been attacked by a Warrior

You can get the same effect with the TXT_KEY_GRAMMER_A_AN construction
Code:
sUnit="UNIT_ARCHER"; print(Locale.Lookup("You have been attacked by {TXT_KEY_GRAMMAR_A_AN << {1}}", GameInfo.Units[sUnit].Description))
 --> You have been attacked by an Archer
sUnit="UNIT_WARRIOR"; print(Locale.Lookup("You have been attacked by {TXT_KEY_GRAMMAR_A_AN << {1}}", GameInfo.Units[sUnit].Description))
 --> You have been attacked by a Warrior

although I think the former is actually easier to understand
 
upper/lower may be of limited use, but really can't think of a need for textkey

Code:
sText="Hello"; print(Locale.Lookup("{1}", sText))
 --> Hello
sText="Hello"; print(Locale.Lookup("{1: upper}", sText))
 --> HELLO
sText="Hello"; print(Locale.Lookup("{1: lower}", sText))
 --> hello
sText="TXT_KEY_CLOSE"; print(Locale.Lookup("{1}", sText))
 --> Close
sText="TXT_KEY_CLOSE"; print(Locale.Lookup("{1: textkey}", sText))
 --> Close
sText="TXT_KEY_CLOSE"; print(Locale.Lookup("{1: lower}", sText))
 --> close
 
Number formatting might be useful, but, as there are no leading spaces/trailing zeros, probably not as useful as using string.format() directly

Code:
fNum = 2.58; print(Locale.Lookup('{1}', fNum))
 --> 2.58
fNum = 2.58; print(Locale.Lookup('{1: number}', fNum))
 --> 2.58
fNum = 2.58; print(Locale.Lookup('{1: number #}', fNum))
 --> 3
fNum = 2.58; print(Locale.Lookup('{1: number #.#}', fNum))
 --> 2.6
fNum = 2.58; print(Locale.Lookup('{1: number #.##}', fNum))
 --> 2.58
fNum = 2.58; print(Locale.Lookup('[[{1: number ##.###}]]', fNum))
 --> [[2.58]]
fNum = 2.58; print(Locale.Lookup('{1: number "#.##"}', fNum))
 --> 2.58
:number without a format string is redundant, as are the quotes around the format string (but see below)

Code:
fNum = 4; print(Locale.Lookup('{1: spellout}', fNum))
 --> four
fNum = -4; print(Locale.Lookup('{1: spellout}', fNum))
 --> minus four
fNum = 2.58; print(Locale.Lookup('{1: spellout}', fNum))
 --> two point five eight

Code:
fNum = 4; print(Locale.Lookup('{1: number "\'positive \'#;\'negative \'#"}', fNum))
 --> positive 4
fNum = -4; print(Locale.Lookup('{1: number "\'positive \'#;\'negative \'#"}', fNum))
 --> negative 4
These format strings need to be quoted, the positive/negative formats are seperated by a semi-colon. Literal text needs to be placed in single quotes. Also note that the number output is math.abs({1}) for both sides of the format.
The obvious use for the positive/negative format is to display a number in green/red, as per the example in the game files
 
Moderator Action: thread moved to the tutorials section :goodjob:
 
Back
Top Bottom