Monday, October 14, 2024

Localization in Blazor

Here are the lessons that I learned while performing the translation to Spanish for The Bed Brigade National website.  I created documentation specific for our project.

  • If you have a brownfield project, you have to find all of the strings that need to be localized in your Razor files.  I ended up creating logic to do this.  
  • You have to localize any validation messages.  I still wanted to use DataAttributes for validation so I ended up writing some custom code to pull out the validation messages and then more custom logic to actually do the validation and get the translation.
  • The need to do partial translations.  Sometimes dropdown lists, enums, or other dynamic content can be partially translated.  I created code to do this.
  • Content Management System translations.  We are seeding our whole site so that when you run it locally or you deploy it to a new environment you have data to work with.  So the content can be pre-translated.  However once the user changes words on the page, we need to check if we can translated it using what we have already translated, if not we need to queue it for translation.  
  • Translators cannot handle weird line feeds in the middle of your content.  I like to format my HTML before I save it and this does not affect the browser rendering at all.  However the language translators have no idea what you are doing and will return back strange translations.  So you have to replace all tabs, and line feeds with spaces.
  • There is one free translator available.  It is slow but it works.
Before embarking on localization in Blazor I watched most of the videos on YouTube relating to localization.  Basically there are two options, the traditional resource .resx files that have been around since WinForm days and resource files using YAML.  I would encourage you to watch both of these videos:  


I chose the second option; the multilanguages open source project to perform localization.  I contributed a bunch of new features to this project which I originally created for the Bed Brigade National website.  The main reason why I chose it was for its ability to translate the YAML files into 69 different languages.  Although my wife speaks Spanish, she did not have to translate a single word for our website.  

Bed Brigade is a charity and I did not want to break the bank on translations.  We ended up going with the free API Layer translator service.  Since everything on the entire site is pre-translated with resource files, it is only when the user adds or changes content that we might need to translate something.

Here are the other options for translating text dynamically if you have a content management system on your site.