Simple translations in C#

A simple method to add translation to your C# code without having to introduce functions call everywhere.

Screenshot of code example further in the text

As a developer, I prefer using English in my application. It’s the one language that most people know and often has shorter words (most popular languages have shorter words, but this is a topic on its own). However, I’m from the Netherlands, and the English words I choose might not always be correct. So when writing software for an English audience, I have a decent chance of making an error. In addition, the users might also not prefer my choice of words. For these reasons, I always describe that the language we as developers use is called iv, which stands for “invariant culture.” It’s me telling our entire team is speaking another language. The benefit is that when we need to use domain-specific language that is hard to translate or maybe doesn’t even exist in another language, we can use the native word for it. The users, who are familiar with the domain, will still understand this mix of languages. Granted, it’s a bit of a cheat, but it works terrific, and I find that more important.

Once the developers have agreed on that, my second goal will be to create a translation system and put that in the hands of the users. That way, they can translate the “invariant culture” to another language, such as English.

I have seen some pretty complex solutions, but I prefer my answer below.

public class TranslationString
{
    private readonly string Value;

    public TranslationString(string value)
    {
        Value = value;
    }

    public static implicit operator TranslationString(string value)
    {
        return new TranslationString(value);
    }

    public static implicit operator string(TranslationString other)
    {
        return TranslationSingleton.Tranlsate(
            UserSingleton.CurrentUser.Locale, 
            other.Value
        );
    }
    
    public override string ToString()
    {
        return (string)this;
    }
}

Note that it does require a singleton, which one might consider a negative, but the beauty is that the moment a developer uses it, there will be a translation. All they need to do is search and replace string with TranslationString. They can either wrap or assign it. The moment it gets assigned to a string or needs to be used as a string it will perform the translation.

TranslationString translation = "Easy Translations!";
UserSingleton.CurrentUser.Locale = "nl-NL";

// Output: "Gemakkelijke vertalingen!"
Console.WriteLine(translation); 

// And the following does the same.
// Output: "Gemakkelijke vertalingen!"
string message = translation;
Console.WriteLine(message);

The beauty is that if we ever miss one, all we need to do is wrap the text at the most appropriate location. It will automatically register the iv-based text as a key, which is, in most cases, more than sufficient. There are a few more complex cases (such as string interpolation). Still, if you start to worry about that, then you also need to think about complex cases as multiple forms of plural (yes, those exist) or the fact that some words change depending on the gender of a person. At the moment you need a complex solution to cover all those complex use cases.