i13n
August 26, 2020Internationalization. That is a long name. It was scary the first time heard about it. It is not. It has to be set up in the project once. Then it is cake walk. Different projects from different frameworks have different ways to configuring this. The essense is the same. We are doing this for strings only. Specifically the strings that are messages. We show strings in text nodes in HTML, in alerts and many places. We put data inside the program. With internalization, we give each string a key. And for each key, we have value sets in multiple languages. Also have a default set of value default language. Suppose we have a web app. We expect out audience to be speakers of English, French and Germen. We will have three files: messages.default, messages.fr and messages.de (de for Germen). Each file will be a key value map. In default file, welcome_message will be “Welcome” in messages.fr, welcome_message will be “Bienvalue” and so on. There will be many message keys for every text we use. “login_failed_error_message”, “purchase_successful_message” and so on. To use the message in the code we just call some function with the key like showAlert( Messages.get(”purchase_successful_message”)). That does the trick. The configuration for which language to use is set outside in the context. A mobile app can detect from the phone’s API, website can get it from the HTTP headers. How all these glue together is managed by a library or framework. That is all. Super simple.