Search code examples
localizationlanguage-agnosticglobalization

When is localization (or lack of) a bad thing?


The following code returns a message which states whether the input value is a palindrome:

(for the sake of this post, the syntax isn't important, or written in any particular language)

function isPalindrome(
    value: string, 
    successMessage: string = "Is palindrome", 
    failureMessage: string = "Is not palindrome"): string {

    return value.reverse() == value ? successMessage : failureMessage;
}

Notice in the code above, the default messages are written in English, however as they are supplied as parameters, this method could easily be localized, and because the method has default values for the messages, this doesn't force the developer to provide them; for example:

isPalindrome("level") // returns "Is palindrome"

However we can demonstrate localization; for example, in spanish:

isPalindrome("level", "es palíndromo", "no es palíndromo") // returns "es palíndromo"

This got me thinking, when should code be designed with localization in mind?

Another example would be with exceptions; for example:

class PalindromeException : Exception("Is not a palindrome")

function validatePalindrome(value: string): void {
    if (value.reverse() != value) {
        throw PalindromeException();
    }
}

Notice in this example that there is no way to localize the message in the exception. I know this could easily be fixed using the same principles in the first example, but it has been purposefully designed to demonstrate a lack of globalization.

Therefore, when should globalization be applied to code, so that it may be localized? When does it matter, and when does it not?


Solution

  • I believe, there's no ideal answer - everything depends on your architecture and use-cases.

    However, I would suggest the following patterns:

    1. All log messages (both server and client) should be in English
    2. All error API responses should always provide a description in English and a unique error code which you can translate into a friendly message on the client side
    3. In order to provide a good UX, all client messages should be in a user's language

    In general, it's a good practice to have all technical data (logs, errors, etc) in English. All user-facing data has to be understandable for a user.