by Philipp Schäfer
While we keep on working on unannounced projects… let’s use the time for another small tech post.
I was surprised these days when I started to build in localization into a bigger project in Unity. I could not find any built-in support for it. The number of plugins concerning this issue gave me the feeling that Unity gave in to the localization-lobby on this behalf. Most of those plugins succeeded wonderfully in making a small task very complicated, including large and unreadable documentations (well done folks!).
Recently we released an article on Making Games about our own engine in Schein. Here is a lesson learned: You can do it yourself! It’s not a good idea every time but for such a small task it sure is. In the end I don’t want to do a text adventure – I just want to translate a menu. In this minimal case a clean and straight forward approach is sufficient.
Let’s start by creating a table with an identifier string in the first column and the related texts in all languages in the next ones. A csv file format is a good choice, since a reader is written in no time using a tokenizer in C# – or you can just google for one and find plenty (go for the smallest code size it should be no more than 2 little functions). A row is normally defined by a page break “n” and columns, like the file format “comma-separated values” says, by a “,” or “;”. Most cases are covered by this regular expression:
Now we add the script and the file to our scene, hand the file to the script and read the data into a static 2D array. Another small script and we are finished. A script that grabs every text element that is appended to its gameobject and replaces the string with the current language. If you want to change the language just iterate over all your translators and replace the old string with the new one.
Happy? I am! No additional download, no unknown code, most time was spent evaluating plugins … in the end all I needed were 5 functions and one table.
Possible improvement? Of course! If you have many text parts in your project you should use numeric IDs to find the right text without performance sucking string compares. If you have many and long texts you should of course load them on demand and not everything at the start.
Questions? Just mail me.