On demand localization instead of forced at init section in DefaultTranslator
Original Reporter info from Mantis: dezlov
-
Reporter name: Denis Kozlov
Original Reporter info from Mantis: dezlov
- Reporter name: Denis Kozlov
Description:
DefaultTranslator unit is great for enabling localization in projects. It has however a major drawback. It calls SetDefaultLang('') at initialization section, which means that any application that imports this unit get the benefit of localization but without much control over the selected language.
Please remove SetDefaultLang('') from initialization section, so that the application can control which language to load or whether to load it at all. Please keep reading for a more detailed explanation and a non-breaking implementation.
--
SetDefaultLang('') picks up the language from system locale, LANG environment variable and --lang command line option. These are great, but again provide no control for the application over which language is loaded. The reason why it is called from initialization section is to make sure that no forms have been created, at a cost of losing control.
Automatically detecting the desired language does not always produce desired results for the end user. Many users have systems with different locales to what their proffered UI language is, while others prefer to use English UI language despite the locale setting, etc.
Calling SetDefaultLang multiple times is not a good option. Apart from obvious overhead, if the second call uses a language file with partial translations then non-translated visual components will still contain a language loaded during automatic translation at the initialization stage, instead of the original text.
Applications normally give users an option to select their preferred language, so it should be able to load a user selected language at start-up. Alternatively, if user has not chosen any language yet - then load an automatically detected one.
--
There is a way to implement this without breaking compatibility for applications which rely on SetDefaultLang being called at initialization stage.
- Remove SetDefaultLang('') from initialization section in DefaultTranslator.pas.
- Rename DefaultTranslator.pas to DefaultTranslatorBase.pas (or similar).
- Create an empty DefaultTranslator.pas unit which simply imports DefaultTranslatorBase.pas and calls SetDefaultLang('') at initialization stage.
This gives the best of both, by keeping original operation of DefaultTranslator and allowing applications to control the loaded language by importing DefaultTranslatorBase instead of DefaultTranslator.
But if backwards compatibility is not important in this case - then simply remove SetDefaultLang('') from initialization section.
Mantis conversion info:
- Mantis ID: 26631
- Build: Lazarus 1.2.4 r45510 FPC 2.6.4
- Version: 1.2.4
- Fixed in version: 1.4
- Fixed in revision: 47310 (#8664b260)
- Monitored by: » dezlov (Denis Kozlov)