2

I have a project in MVC 3 (Razor) For localization we are using Strongly typed resources. We want to have possibility to update translation that already exist "on-line". It means, that it should be possible to edit translation on the website. (e.g. If in the url there is parameter like "translateLanguage=on") Basically, it is not possible to do that with current solution, because if resource has been changed, then it must be recompiled.

Of course we can write our own Resource Manager that will be using a database, but then we would have to rewrite all of our translations to the database and that would be time consuming. It would also mean that we would have to change all of our code to reflect this "new" resource manager.

It would be hard to implement it in all things. Now, we can use it in attributes e.g.

[Required(ErrorMessageResourceType = typeof(_SomeResource), ErrorMessageResourceName = "SomeResouceElement") 
  SomeProperty

As well as in code:

 string translatedResource = _SomeResource.SomeResourceElement;

Could you provide me with some information how to do this in mvc 3?

tereško
  • 58,060
  • 25
  • 98
  • 150
Piter
  • 29
  • 2
  • Please clarify your question: What is the problem? On which topic do you need information? What about those attributes? – PVitt Oct 17 '11 at 08:29
  • in conclusion, I need to change the default Resource Manager in the manner that no code changes will be needed (in using resource manager in webapplication) All will be coded as it is now (excluding the Resource Manager impelmentation of course) and it will be possible to dynamicaly change resources (they will be not precompiled and embedded into the assembly) – Piter Oct 17 '11 at 09:24
  • Anyone?:> I know it is a hard problem but any clue would be valuable. it must bu possible to change translation "on-line" with MVC :O – Piter Oct 20 '11 at 08:46
  • Sounds like you're wanting to build a drop-in replacement for the built-in ResourceManager; but you haven't said how it might operate, so offering "clues" on how to implement such is impossible. My suggestion: Start by digging into the existing ResourceManager until you have a solid understanding of the default implementation. Then, plan and detail out how your replacement would differ, and how your requirements will be achieved in practice. *Once you understand the problem, you'll be able to ask useful questions about the solution.* Best of luck! – Brian Lacy Jan 08 '14 at 18:20

1 Answers1

2

Generally resource file consists of two parts xml + autogenerated cs code. If you open resource designer file you will see

 /// <summary>
        ///   Looks up a localized string similar to About project.
        /// </summary>
        public static string about_project {
            get {
                return ResourceManager.GetString("about_project", resourceCulture);
            }
        }

So what you can do you can use ResourceManager.GetString("Key")

Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
            var t = Resources.ResourceManager.GetResourceSet(new CultureInfo(cultureName), true, true);

To make it more smart you can rewrite BaseView

public abstract class ViewBase<TModel> : System.Web.Mvc.WebViewPage<TModel>
{
    public string GetTranslation(string key)
    {
        return _rManager.GetString(key);
    }

    private ResourceManager _rManager;
    protected ViewBase()
    {
        _rManager = Resources.ResourceManager.GetResourceSet(new CultureInfo(cultureName), true, true);
    }


}

And then you will be able to use GetTranslation in your razor view (To run this base view you need to modify web.config from Views folder)

And then you will be able after editing xml access to resource data.

Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80
  • Unfortunately this will not solve @Piter's problem as he has references to strongly typed resources eg. `_SomeResource.SomeResourceElement` – polkduran Jan 14 '14 at 16:47
  • @polkduran well but then its impossible to solve since each time need to be rebuilded, but if he can use string thenGetTranslation("SomeResourceElement") will work – Vova Bilyachat Jan 14 '14 at 18:08