Thoughts from the Wet Coast

The musings of an ASP.NET Developer from Canada's We(s)t Coast

Module Development in DNN 8: 4 - Localization support for MVC modules

Category: DotNetNuke
Last Modified: May 2 2017
Jul 9 2015

DNN_Loves_MVC_LogoThis blog was originally posted on the DNN Community blog

DNN 8 will support creating modules using ASP.NET MVC version 5.1 or later.  In previous blogs in this series I showed how you can set up your environment to create MVC modules and I created my first MVC Action/View. In this blog I will describe the support we have included for localizing text in MVC Controllers and MVC Views.  As before the source code for my examples is available on Github at https://github.com/cnurse/DnnConnect.Demo.

MVC Controllers

In my earlier blog I described how DNN provides a base Controller (DnnController) that developers can sub-class, when building modules using ASP.NET MVC. This base class provides Portal and Module context as well as some helper methods – just like PortalModuleBase does for “classic” WebForms based DNN modules.

Localization is provided by two members of the base class – a LocalizeString method and a LocalResourceFile property.

Listing 1:  Localization support in DnnController base class

   1:  public string LocalResourceFile { get; set; }
   2:   
   3:  public string LocalizeString(string key)
   4:  {
   5:      return Localization.GetString(key, LocalResourceFile);
   6:  }

The LocalizeString method is similar to the LocalizeString method in PortalModuleBase.  It calls the Localization class passing the key and the LocalResourceFile.  The LocalResourceFile name can be anything you want, but by convention it defaults to <>.resx.  In the case of the example project which has a single controller – ContactController, the default LocalResourceFile value is Contact.resx.

There isn’t an example of the use of LocalizeString in the example project – you are most likely to need localized strings in the UI, so there is also support for localization in your MVC Views.

Dnn View Helper

As mentioned in the earlier blog DNN also provides a default base View – DnnWebViewPage.  As long as you make your views inherit from this base WebViewPage (or its generic equivalent DnnWebViewPage) then you will have access to a Dnn helper class.

The Dnn helper class also supports the same two members – so you can use the helper syntax @Dnn.LocalizeString(key) in your Razor views.  As before you can set the LocalResourceFile property or use the default convention which is the same as before, i.e. the <>.resx, where <> is the parent Controller class for the view.

The sample code includes the use of this method in both of its views (see Listing 2)

Listing 2: Use of the Dnn Localization helper

   1:  <div>
   2:      <span>@contact.FirstNamespan>
   3:      <span>@contact.LastNamespan>
   4:  div>
   5:  <div>
   6:      <span>@Dnn.LocalizeString("Email")span>
   7:      <span>@contact.Emailspan>
   8:  div>
   9:  <div>
  10:      <span>@Dnn.LocalizeString("Phone")span>
  11:      <span>@contact.Phonespan>
  12:  div>
  13:  <div>
  14:      <span>@Dnn.LocalizeString("Twitter")span>
  15:      <span>@contact.Twitterspan>
  16:  div>

That’s it – we have tried to keep the experience familiar to DNN developers while embracing MVC standards.  Please give it a try and tell us what you think.


For more information

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Categories

Tags