Forcing/defaulting to a specific language (culture) per-user based on custom profile setting

This is a place to discuss how to adapt mojoPortal to the needs of different cultures. Before asking questions here please first review the localization documentation.

This thread is closed to new posts. You must sign in to post in the forums.
1/7/2010 9:37:17 PM
Gravatar
Total Posts 31
There are 10 kinds of people in the world, those who know binary, and those who don't

Re: Forcing/defaulting to a specific language (culture) per-user based on custom profile setting

Thanks a lot

7/22/2011 5:39:28 AM
Gravatar
Total Posts 5

Re: Forcing/defaulting to a specific language (culture) per-user based on custom profile setting

Since I needed this also, here is an implementation:

Custom profile setting:

<add name="Language"
    type="System.String"
    allowMarkup="false"
    resourceFile="CountryISOCode2Resources"
    labelResourceKey="LanguageLabel"
    lazyLoad="false"
    requiredForRegistration="false"
    showOnRegistration="false"
    allowAnonymous="false"
    visibleToAnonymous="false"
    visibleToAuthenticated="true"
    visibleToUser="true"
    editableByUser="true"
    regexValidationExpression=""
    regexValidationErrorResourceKey=""
    onlyAvailableForRoles=""
    onlyVisibleForRoles=""
    defaultValue="vi-VN"
    includeHelpLink="false"
    >
   <OptionList>
    <Option value="vi-VN" TextResourceKey="CountryLabelVietNam"></Option>
    <Option value="en-US" TextResourceKey="CountryLabelUnitedStates"></Option>
   </OptionList>
  </add>

and IHTTPHandler:

using System;
using System.Globalization;
using System.Threading;
using System.Web;
using mojoPortal.Business;
using mojoPortal.Web;

namespace CustomCultureForUser
{
   public class UserSettingCultureHelperHttpModule : IHttpModule
   {
      public void Init(HttpApplication application)
      {
         application.PostAuthorizeRequest += DetermineCulture;
      }

      public void Dispose() { }

      protected void DetermineCulture(object sender, EventArgs e)
      {
         HttpApplication app = (HttpApplication) sender;
         if (!app.Request.IsAuthenticated) return;
         try
         {
            SiteUser siteUser = SiteUtils.GetCurrentSiteUser();
            string langsetting = siteUser.GetCustomPropertyAsString("Language");
            if (!string.IsNullOrEmpty(langsetting))
            {
               CultureInfo ci = new CultureInfo(langsetting);
               Thread.CurrentThread.CurrentCulture = ci;
               Thread.CurrentThread.CurrentUICulture = ci;
               return;
            }
         }
// ReSharper disable EmptyGeneralCatchClause
         catch
// ReSharper restore EmptyGeneralCatchClause
         {
           //Ignore errors. Nothing we can do anyway
         }
      }
   }
}

I had to add LanguageLabel to CountryISOCode2Resources.resx

<data name="LanguageLabel" xml:space="preserve">
    <value>Language</value>
  </data>

7/22/2011 5:44:05 AM
Gravatar
Total Posts 5

Re: Forcing/defaulting to a specific language (culture) per-user based on custom profile setting

<add name="UserSettingCultureHelperHttpModule" type="CustomCultureForUser.UserSettingCultureHelperHttpModule, CustomCultureForUser" preCondition="managedHandler" />

I forgot the config for the handler. It has to added in ADDITION to the existing culture handler (not replace the existing)

Thus,
1. If a user has a custom culture defined, it is used as the first preference
2. If a user does not have a custom profile, it defaults to the culture of the site
3. If the site does not have a profile, it uses the web application's setting

Technically, it will first determine the culture in 3. Then it will invoke the mojoportal ihttphandler, which will do 2, thereby possibly overwriting the culture. And then it will run my ihttphandler, possibly overwriting the culture if the user has a setting for it. So in various stages of the request, it may end up with 3 different cultures. In reality this approach just makes the implementation simpler and works perfectly.

 

You must sign in to post in the forums. This thread is closed to new posts.