August 24, 2012

Portal Access Based On User Locale in DNN 6.2.0


We were trying to figure out how to redirect a user to the DNN 6.2 by using their locale set at the profile through code. There was no way was available to do this, but finally did a workaround.

If you have enabled localization in your site, once you logged as host, then In the Logged In User view you will see the English, and other languages installed. If that has been set lets say to French, then all the site users will have the chnace to chnage their language by using this Logged In User section. But what if we want to navigate users to the site using their language set in the profile.

I tried many ways but their is no way that I could find to set the language by user locale. So as a workaround, used the following code sample to direct the user by locale.


int tabId = new TabController().GetTabByName(pageName, this.PortalId).TabID;//get the current page tab id

string targetUrl = Globals.NavigateURL(tabId); // get the url set
Dictionary<string, Locale>.ValueCollection locales = LocaleController.Instance.GetLocales(this.PortalId).Values; // get all the locales installed in the machine
int count = 0;
///Loop through all the locales and get the user locale and replace it
///with the locale added to the current
if (!String.IsNullOrEmpty(user.Profile.PreferredLocale))
            {
                foreach (Locale locale in locales)
                {
                    if (targetUrl.Contains(locale.Code))
                    {
                        targetUrl = targetUrl.Replace(locale.Code, user.Profile.PreferredLocale);
                        break;
                    }
                    if (targetUrl.Contains(locale.Code.ToLower()))
                    {
                        targetUrl = targetUrl.Replace(locale.Code.ToLower(), user.Profile.PreferredLocale.ToLower());
                        break;
                    }
                    if (targetUrl.Contains(locale.Code.ToUpper()))
                    {
                        targetUrl = targetUrl.Replace(locale.Code.ToUpper(), user.Profile.PreferredLocale.ToUpper());
                        break;
                    }
                }
            }


Response.Redirect(targetUrl);


This is only a workaround for this issue. :)

No comments:

Post a Comment