ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Tutorials & Other Helpful Info (https://www.esoui.com/forums/forumdisplay.php?f=172)
-   -   Singular & plural form using the zo_strformat() function (https://www.esoui.com/forums/showthread.php?t=1443)

Garkin 05/09/14 05:13 AM

Singular & plural form using the zo_strformat() function
 
Lua Code:
  1. pattern = "I have sold <<1>> <<1[items/item/items]>>."
  2.  
  3. msg = zo_strformat(pattern, num)

If num = 0
<<1>> is replaced by num and <<1[items/item/items]>> returns first from
msg = "I have sold 0 items"

if num = 1
<<1>> is replaced by num and <<1[items/item/items]>> returns second from
msg = "I have sold 1 item"

if num > 1
<<1>> is replaced by num (eg 5) and <<1[items/item/items]>> returns third from
msg = "I have sold 5 items"

slowglass 05/09/14 05:57 AM

You can also do
Code:

pattern = "I have sold <<1[no items/a single item/$d items]>>."
msg = zo_strformat(pattern, num)

If num = 0
msg = "I have sold no items"

if num = 1
msg = "I have sold a single item"

if num > 1 (ie 5 in this case)
msg = "I have sold 5 items"

Garkin 05/09/14 06:05 AM

When we define just 2 forms in pattern, it means that first form is for num = 1 and second form for anything else. So, this pattern will have the same output as in example above:

Lua Code:
  1. pattern = "I have sold <<1>> <<1[item/items]>>."


We can modify pattern even further using the $d variable:
Lua Code:
  1. pattern = "I have sold <<1[$d item/$d items]>>"
$d will get replaced with actual value of argument, so output will be the same as in both examples above.


Also we can leave empty any of the forms, so there will be no return value. Good example is time format:
Lua Code:
  1. pattern = "Remaining time: 1[/1 minute and /$d minutes and ]>><<2[1 second/$d seconds]>>"
  2.  
  3. msg = zo_strformat(pattern, min, sec)

As we do not have defined form for zero value for minutes, output will be:
if min = 0, sec = 1
msg = "Remaining time: 1 second"

if min = 0, sec = 5
msg = "Remaining time: 5 seconds"

if min = 5, sec = 0
msg = "Remaining time: 5 minutes and 0 seconds"

Xrystal 05/09/14 06:14 AM

Thanks for this information. I saw those strings in the zgoo display but couldn't figure out how they are used.

CrazyDutchGuy 05/09/14 08:00 AM

Garkin pointed me helpfully to this information at my addon.

I tried figuring out how it would work correclty with the plurality forms of item links and i couldn't figure it out how it works properly.

For example

the m: would define the plural form of the item

Code:

zo_strformat("<<1>> <<2>> <<m:3>>", player, quantity, itemlink)
the question i couldn't figure out is how to define the plural form of the item based on the quantity. The strings in zgoo display just generally use the single form.

Seerah 05/09/14 09:09 AM

Beautiful, Garkin. :)

Iyanga 05/09/14 09:11 AM

Reading the thread title I had hoped to get an answer for a slightly different problems.


Some words/strings are localized in singular and some in plural form.

How can you modify the string depending on the form?

Basically I need something like:

Lua Code:
  1. if localizedword == singular then
  2. auxVerb = "xyz"
  3. elseif localizedword == plural then
  4. auxVerb == "abc"
  5. end
  6. string.format("bla bla %s %s", auxVerb , localizedword)

Yes, that's the way I can do it, I was wondering though if there is a smart zo_strformat solution. There are fragments like <<1[/m/f]>> in EsoStrings, which looks like there is a way, but no idea how they need to be used and combined properly.

Basically something similar to the solution here, just without a number involved.

Garkin 05/09/14 10:42 AM

There are also other control characters, but without documentation is hard to say if I got them all and if it is correct:

a: - adds indefinite article in correct form ("a/an") if string 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,^N)
c: - converts first character to lower case
C: - converts first character to upper case, used to display names
d: - demonstrative pronoun, adds "this" if string is not name (does not contain ^M,^F,^N)
D: - returns demonstrative pronoun
g: - adds " of a", if string is name it will add to the end "'s" for singular, "'" for plural
G: - adds " of the" if string is name it will add to the end "'s" for singular, nothing for plural
i: - changes number to index (1st,2nd,3rd,....100th)
I: - the same as above?
l: - adds "at the ", used with locations
L: - adds "to the ", used with locations
m: - multiplication, displays singular or plural accoding to the second argument. If you have 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, just first letter is capital (One,Two,...)
o: - possessive pronoun (subject). Returns his, her, its according to the ^m, ^f, ^n on the end of string
O: - possessive pronoun (object). Returns his, hers, its.
p: - personal pronoun (subject). Returns he, she, it accoding to the ^m, ^f, ^n on the end of string
P: - personal pronoun (object). Returns him, her, it.
r: - reflexive pronoun. Returns himself, herself, itself accoding to the ^m, ^f, ^n on the end of string
R: - number to roman numerals.
t: - converts first letter to upper case in all words that have more than 1 character (used by default to format item links)
T: - converts first letter to upper case in all words (including single letter words)
x: - changes link to plain text (EDIT: as of Update 3 this doesn't work)
X: - raw format (probably, it does not remove ^ control chars from strings, links are unchanged.)
z: - converts text to lower case letters, works for special characters like ÁÉÄËÍÏ etc.
Z: - converts text to upper case letters, works for special characters like áäéëíï etc.

- control characters are used in form <<Z:1>> or <<1{Z}>>
- instead of <<2>><<m:1>> can be used simplified form <<2*1>>

Almost all control charactes can be used together, so:
zo_strformat("<<C:1>> <<2>> <<tm:3>>", "i have", 10, "blue colored item")
will return "I have 10 Blue Colored Items"

If you want to work with translations, there are a few control characters that you can add to the end of 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")
returns "Peter's item"

Garkin 05/09/14 11:17 AM

Another catch:

<<player{first item/second item}>>
returns first item when player character is male, second item if character is female.

<<1{first/second/third}>>
returns first if argument is masculine, second if femine and third if plural.

Iyanga 05/09/14 11:45 AM

Quote:

Originally Posted by Garkin (Post 7322)

<<1{first/second/third}>>
returns first if argument is masculine, second if femine and third if plural.

plural? Either neutral or some strings are broken. Using the POI "Tangle Rock" on this returns the third argument.

Iyanga 05/09/14 12:24 PM

Quote:

Originally Posted by Garkin (Post 7321)
A: - adds definite article if string is not name (does not contain ^M,^F,^N)

Hm.

"Magiergilde^fd" (Mages guild skill line name) together with <<1>> results already in a definite article added -> "die Magiergilde"

Trying to get rid of it :mad:


Update:

I just tried the stupid programmer approach and did:
<<!A:1>> (not A) - and it actually worked and removed the definite article :D :banana:

Garkin 05/10/14 12:57 PM

Quote:

Originally Posted by Iyanga (Post 7324)
plural? Either neutral or some strings are broken. Using the POI "Tangle Rock" on this returns the third argument.

Lua Code:
  1. local fmt = <<1{first/second}>>
  2. msg = zo_strformat(fmt, text)
if text == "test^m"
returns "first"

if text == "test^f"
returns "second"

if text == "test^n"
returns "second"

if text == "test^p"
returns "second"

Lua Code:
  1. local fmt = <<1{first/second/third}>>
  2. msg = zo_strformat(fmt, text)
if text == "test^m"
returns "first"

if text == "test^f"
returns "second"

if text == "test^n"
returns "first"

if text == "test^p"
returns "third"

Lua Code:
  1. local fmt = <<1{first/second/third/fourth}>>
  2. msg = zo_strformat(fmt, text)
if text == "test^m"
returns "first"

if text == "test^f"
returns "second"

if text == "test^n"
returns "third"

if text == "test^p"
returns "fourth"

Garkin 05/10/14 01:03 PM

Quote:

Originally Posted by Iyanga (Post 7327)
Hm.

"Magiergilde^fd" (Mages guild skill line name) together with <<1>> results already in a definite article added -> "die Magiergilde"

Trying to get rid of it :mad:


Update:

I just tried the stupid programmer approach and did:
<<!A:1>> (not A) - and it actually worked and removed the definite article :D :banana:

I was wondering what it means "<<!aC:1>>" in german EsoStrings... :)

Iyanga 05/18/14 11:38 AM

And another note:

Using m with items is not worth the time, there is no way to get correct output for German.

There seems to be an algorithm if the string has no ^p version to turn a normal string into a plural string, i.e. to add the letter "e" or "n". But the algorithm fails very often.

The results for example:
"Backfett" -> "Backfette" [OK]
"Käse" -> "Käsee" [NOK]


All times are GMT -6. The time now is 01:00 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI