How to format strings with zo strformat

From ESOUI Wiki

Jump to: navigation, search

The ESO API provides a very useful function called 'zo_strformat'.

With it you can format strings with interpolation, pluralization, localization and many other features.

zo_strformat(format[, arg1[, arg2] ...])

The first argument is the actual string, and the following optional arguments are the parameters to be included in the string.

zo_strformat("Hello, <<1>>!", "player") --> Hello, player!

The syntax '<<n>>' is used as placeholder for the replacement value, where the number 'n' is the position of the argument when you called the function.

zo_strformat("<<2>>, <<1>>!", "you", "Hey") --> Hey, you!

There's special syntax for pluralization.

zo_strformat("We've seen <<1[no examples/one example/$d examples]>>", 1) --> We've seen one example.
zo_strformat("We've seen <<1[no examples/one example/$d examples]>>", 0) --> We've seen no examples.
zo_strformat("We've seen <<1[no examples/one example/$d examples]>>", 3) --> We've seen 3 examples.

There's also another special syntax called modifiers. For instance, the modifier 'l' prepends "at the " to the value.

zo_strformat("I'm <<l:1>>.", "shrine") --> I'm at the shrine.

There's a myriad of modifiers you can use, check out the table below:

Modifier Effect
a Adds indefinite article in correct form ("a/an") if the value is not name (does not contain ^M, ^F, ^N). If used with 'm' (<<ma:1>>) it will add "some".
A Adds definite article if string is not name (does not contain ^M, ^F, or ^N).
c Converts the first character to lower case.
C Converts the first character to upper case, used to display names.
d Demonstrative pronoun; adds "this " if string is not a name (does not contain ^M, ^F, ^N).
D returns demonstrative pronoun
g Appends " of a". If string is a name it will append "'s" for singular, or "'" for plural
G Appends " of the". If string is a name it will append "'s" for singular, or nothing for plural
i Changes number to ordinal (1st, 2nd, 3rd, ..., 100th)
I  ?
l Prepends "at the ", used with locations.
L Prepends "to the ", used with locations.
m Multiplication, displays singular or plural according to the second argument. If you have a number on any other position, this will not work.
n Number to text (one, two, three, ..., twelve). Does not work for numbers higher then 12.
N Same as above. But the first letter is capital (One, Two, ..., Twelve)
o Possessive pronoun (subject). Returns "his", "her", or "its" according to the gender (^m, ^M, ^f, ^F, ^n, or ^N at the end of the string)
O possessive pronoun (object). Returns "his", "hers", or "its" according to the gender (^m, ^M, ^f, ^F, ^n, or ^N at the end of the string)
p Personal pronoun (subject). Returns "he", "she", or "it" according to the gender (^m, ^M, ^f, ^F, ^n, or ^N at the end of the string)
P Personal pronoun (object). Returns "him", "her", or "it" according to the gender (^m, ^M, ^f, ^F, ^n, or ^N at the end of the string)
r Reflexive pronoun. Returns "himself", "herself", or "itself" according to the gender (^m, ^M, ^f, ^F, ^n, or ^N at the end of the string)
R Number to roman numerals (I, II, III, ..., XII)
t Converts the first letter to upper case in all words that have more than 1 character (used by default to format item links).
T Converts the first letter to upper case in all words (including single letter words).
X Raw format (probably, it does not remove ^ control chars from strings, links are unchanged).
z Converts to lower case letters, works for special characters like ÁÉÄËÍÏ etc.
Z Converts to upper case letters, works for special characters like áäéëíï etc.
  1. Modifiers can be used in two forms: '<<Z:1>>' or '<<1{Z}>>'
  2. Instead of '<<2>><<m:1>>', it can be simplified to '<<2*1>>'

Almost all modifiers can be used together, so:

zo_strformat("<<C:1>> <<2>> <<tm:3>>.", "i have", 10, "blue colored item") -- I have 10 Blue Colored Items.

If you want to work with translations, there are some control characters that you can add to the end of a string:

^f Femine gender
^F Femine name
^m Masculine gender
^M Masculine name
^n Neuter gender
^N Neuter name
^p Plural
^P Plural name

Example:

zo_strformat("<<g:1>><<2>>.", "Peter^M", "item") -- "Peter's item."

[1] Information originally provided by Garkin here http://www.esoui.com/forums/showpost.php?p=7321&postcount=8

Personal tools
Namespaces
Variants
Actions
Menu
Wiki
Toolbox