Just a note about string concatenation & localization
yaiws-com opened this issue · 7 comments
Concatenation happens before replacement. Given: "$HELLO" is key to the string "Hello".
Debug.Trace( "$HELLO" + " world" )
Results in "$HELLO world" and not "Hello world". If you need concatenation for localized strings store each portion as a string var and use that.
String sConcatHello = "$HELLO"
Debug.Trace( sConcatHello + " world" )
String sConcatHello = "$HELLO"
Debug.Trace( sConcatHello + " world" )
Would log
$Hello world
However, if you sent this to the UI (e.g. AddHeaderOption(sConcatHello + " world")
) and had your translation file as
$Hello Bonjour
$Hello world Hi to the world
it would display as
Hi to the world
since the string it was sending to the UI would be "$Hello world" which is present in the translation table.
The translation isn't done in Papyrus, that wouldn't be sensible because of string caching and case insensitivity. The "translate strings" (i.e. those beginning with a $) are sent to the Scaleform VM and processed there for translation when they're set to a text field.
What is it you are wanting to translate? We (schlangster) recently implemented a, currently undocumented, nested translation system which may be of use.
Well. Something fairly simple. I have text that is concatenated with integers and floats.
string Function decimalDaysToString( Float afDays )
Return Math.Floor( afDays ) + "$DAYS" + (( afDays * 24.0 ) as Int % 24 ) + "$HOURS"
EndFunction
Ah yes, I never thought of something like that!
Leave it with us and we'll see if there's a neat way you can do it.
Cool. Thanks!
Ok here we go, in your translate files have something like
$MYMOD_DAYS{}_HOURS{} Days: {}, Hours: {}
Then in your code, use a function like this to generate your translate string,
string function translateDaysHours(float a_days)
; This function returns the string $MYMOD_DAYS{days}_HOURS{hours}
return "$MYMOD_DAYS{" + Math.floor(a_days) as string + "}_HOURS{" + (( a_days * 24.0 ) as int % 24 ) as string + "}"
endFunction
and when using something like AddHeaderOption()
you can do
float days = 125.4546
...
AddHeaderOption(translateDaysHours(days))
Basically, the {} work as delimiters for replacers, it's a vulgar type of a format string where anything within the curly braces will replace the curly braces in the translate string's value after being, themselves, passed through the translator. We use it for our default text, to avoid having lots of unique translate strings: Our translate files contain $SKI_INFO1{} Default: {}
So we can do things like SetInfoText("$SKI_INFO1{0}")
and it will display Default: 0
or even SetInfoText("$SKI_INFO1{$On}")
so it yields Default: On
I hope this makes sense!
Actually, it makes perfect sense. Thanks!
Excellent 😄