For the moment I'm using razor as my view engine in Nancy.
I can access my resource file like this in razor:
@Text.text.greeting
But I would like to switch to a different view engine.
Are there other view engines available that support the TextResource?
How does localization works in the super simple view engine?
Or is there a way to access the resources using the model?
I now made my own solution because I couldn't use resource files.
In my model I have a dynamic Text object with the resources in the correct language.
(the language depends on the current user and is a int)
At the start I build a static dictionary per language.
I created a ResourceDictionary to fill the dynamic object:
Good question! This is something that I have needed to do myself.
I managed to solve this problem using the basis of the suggestion made to you by @Karl-Johan Sjögren - i.e. I was able to create an extension to the Super Simple View Engine (SSVE).
The Background
The SSVE has been designed in such a way that you can inject additional 'matchers' that allow you to do some processing on your view templates as they are getting rendered for the output of a request.
You will notice the following constructor (as of 2014/05/12) within the SSVE, that allows you to pass in additional 'matchers':
The basic way that most of the template substitution works in the SSVE is by doing very simple regular expression matches against the view templates. If a regular expression is matched, then a substitution method is invoked within which the appropriate substitution occurs.
For example, the default PerformSingleSubstitutions processor/matcher that comes with the SSVE is used to do your basic '@Model.' substitutions. The following processor workflow could occur:
The Implementation
Ok, so now that we have the foundation, here is how you can create your very own Translation matcher. :)
First you will need to create an implementation of ISuperSimpleViewEngineMatcher. Below is a really basic example I have created for the purpose of illustration:
Okay, so when the above matcher is run against our view templates they will to find strings starting with '@Translate.'. The text just after the '@Translate.' is considered to be our translation key. So in the e.g. of '@Translate.Hello_World', the translation key would be 'Hello_world'.
When a match occurs the replace method is fired to find and return the appropriate translation for the translation key. My current example will only return a translation for the key of 'Hello_World' - you would of course have to fill in your own mechanism with which to do the translation lookups, perhaps farming off to the default resource management support of .net?
The matcher won't get automatically hooked up into the SSVE, you will have to use the IoC supported features of Nancy to register your matcher against that constructor parameter I highlighted earlier.
To do so you will need to override the ConfigureApplicationContainer method within your Nancy bootstrapper and add a registration similar to the one below:
The final step is to actually add your translation tokens to your views:
As I said, this is a very basic example which you could use as the basis to create an implementation to suit your needs. You could for example extend the regular expression matcher to also take into account the target culture that you would like to translate into, or just simply use the current thread culture registered within your application. You have the flexibility to do as you please. :)