Alternative to MarkupExtension inheritance in Silverlight
I'm using MarkupExtension inheritance in my WPF app, which I'd like to port under Silverlight.
I've made a "Locale" markup extension which can be used like this in xaml
<TextBlock Text="{ui:Locale aRandomId,'My default translation'}" />
In fact, this MarkupExtension is binding the property its applied on to the string "My default translation", or, if found, to a开发者_如何转开发 sentence stored in a database named "aRandomId".
This MarkupExtension allows me to dynamicaly change my UI's locale (the markupextension is registering to a weak event 'LocaleChanged' which triggers update of every property bound with it)
As we cannot inherit from MarkupExtension in Silverlight, I'd like to know if there is some mechanism which could replace it.
Thanks.
PS : I can provide the source of this MarkupExtension if needed.
I know this will get a lot of traditional answers, but I would also like to put forward something completely original we tried (and succeeded) doing ourselves for more efficient localisation of Silverlight using Attached Properties instead of binding:
Link: Localisation of Silverlight projects after completion
We found resources and binding solutions to be tedious and not flexible as they all require the translations to be set in the project (and therefore needs a re-deploy of the app when it changed).
Also the selected language hardy ever changes and when it does it is all strings at once. Carrying the overhead of bindings for that purpose seems insane when you can just regenerate the current page if the language changes.
Traditionally translations are sent out in batches and managed by language (with unique keys, translator notes etc) so we found this a much better fit there too.
Our solution means we manage the localisation in a central server database but can also provide localisation editor, within the application itself, for translators to use while using the application.
- Central repository for any number of languages
- Change language by downloading new data (max 200kb per language on large app)
- Only download language required and change on the fly
- Supports in-app editing of resources by translators (does require extra UI forms).
For that exact purpose, I'm using a Converter and a ConverterParam:
MSDN Binding.Converter
It's quite easy to do, but it feels very boilerplate when you begin to have a lot of them in your app.
Update: Maybe I'm overcomplicating your needs... In my apps, I have two usage patterns, one with a Converter and one without. In the simplest case, I just want to pull a string for the localized dictionary, and it looks like:
Text="{Binding Localization[my_string_key], Source={StaticResource ViewModelLocator}}"
精彩评论