How to make user text translatable
Last updated at 8:12 pm UTC on 25 July 2004
Question: April 09, 2004 Is there a how-to document about making code translatable ?
Answer: Diego Gomez Deck For the 90% of the cases you only need to send #translated to every visible String. When #translated is evaluated, the receiver (one string) answers the version of this string/phrase in the current language. So,
"answer 'casa' when Spanish is the current language"
'house' translated.
The other useful message (needed to cover the missing 10%) is #format:. String>>format: is a type of "printf()" with a really simple (aka stupid) syntax. The occurrences of '{N}' will be replaced by the string representation of the object at position N in the given SequenceableCollection, so:
"evaluates to 'My name is Diego and I'm 31 years old'
when anObject is me"
'My name is {1} and I'm {2} years old'
format: {anObject name. anObject age}.
The goal with format is to replace former string concatenations like:
'My name is ', anObject name,
' and I'm ', anObject age asString, ' years old'.
Where we need to translate mix constants and variables. As you can see, the order used in the concatenation above makes sense only in English. Since, the #format: replaces the symbolics based on position, the translator can rearrange the places where the expansion occurs.
Combining #translated and #format: you can translate almost any piece of code, For example:
'My name is {1} and I'm {2} years old' translated
format: {anObject name. anObject age}.
Question: How do I install translation capablitity and get it going in my image?
Answer:
- Open the Package Loader from the world menu (See SqueakMap) e (you can install squeakmap through the "Welcome to " window that appears when starting squeak)
- Install "Squeak Multilingualization" package from the Package Loader (find the package on the left pane, right click to get the menu and click install)
- Download a file with the appropriate transalation for example http://homepages.vype.de/sebastian/squeak/EToyVocabulary-addGermanVocabulary.st
- Install the file using the file browser (you may need to remove any XML tags at the beginning or end)
- Again from the squeak world menu, choose help -> Set Language -> Deutsch
- Verify that the changes work: e.g. doit on "append" translated should give "hangen an"