Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Santhosh 58 posts 99 karma points
    Feb 17, 2011 @ 10:10
    Santhosh
    0

    ASP.Net Usercontrol (Resource files) localization with umbraco

    Hi Everyone!

    Now I try to describe my problem... Hope somebody can help me!

    I need to localize asp.net user control, which used on one of umbraco pages. I have almost no experience in development for umbraco, so I tried to localize it in .NET way.

    In MyControl.ascx markup:

    <asp:Localize meta:resourcekey="Title" runat="server"/> 

    And in behind code:

     protected void Page_Load(object sender, EventArgs e)
    {
     
    var language = page.Request.QueryString.Get(GeneralResources.LanguageParam);
               
               
    // if no 'lang' param in request then try to get it from session
               
    if (String.IsNullOrEmpty(language))
               
    {
                    language
    = (string)page.Session[GeneralResources.LanguageParam];
               
    }

               
    CultureInfo culture;
               
    string cultureString;
               
    switch (language)
               
    {
                   
    case GeneralResources.DeValue:
                        culture
    = CultureInfo.CreateSpecificCulture("de-DE");
                        cultureString
    = "de-DE";
                       
    break;
                   
    case GeneralResources.DaValue:
                        culture
    = CultureInfo.CreateSpecificCulture("da-DK");
                        cultureString
    = "da-DK";
                       
    break;
                   
    default:
                        culture
    = CultureInfo.CreateSpecificCulture("en-US");
                        cultureString
    = "en-US";
                       
    break;
               
    }
               
    Thread.CurrentThread.CurrentCulture = culture;
               
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureString);
                page
    .Culture = cultureString;
                page
    .UICulture = cultureString;
               
               
    LanguageLabel.Text = "UICulture: " + Page.UICulture;
               
    LanguageLabel.Text += ". Culture: " + Page.Culture;
       
    //...
    }

    There are files MyControl.ascx.resx, MyControl.ascx.de-DE.resx, MyControl.ascx.en-US.resx MyControl.ascx.da-DK.resx in App_LocalResources folder.

    LanguageLabel on page shows right culture, but localized values load from MyControl.ascx.resx. If I delete MyControl.ascx.resx then no text is showing on page at all!

    Can anybodysuggest how to localize usercontrol with umbraco? I believe this question was posted previously, but i couldnt get the solution.

    Thanks in Advance
    Regards,

    Santhosh

  • kows 81 posts 151 karma points c-trib
    Feb 17, 2011 @ 11:00
    kows
    0

    i use dictionaryitems for this,

    quickly (not foulproof-version):

     

    int languageId;
    string pageId = HttpContext.Current.Items["pageID"] as string;
                if (pageId != null)
                {
                    Domain[] domains = umbraco.library.GetCurrentDomains(int.Parse(pageId));
                    if (domains != null && domains.Length >= 0)
                        languageId = domains[0].Language.id;
                }

     

    string key = "LabelEmail";
    umbraco.cms.businesslogic.Dictionary.DictionaryItem(key).Value(languageId);
  • Santhosh 58 posts 99 karma points
    Feb 17, 2011 @ 13:14
    Santhosh
    0

    Kows, Thanks for a quick reply.

    But let me put my question in more detail. i have an user control, named OrderingControl.ascx. I need to use resource files to have all the text's coming from the respective resource files. What I would need is, at runtime My site will get the culture info. Which I need to set to the user control so that appropriate resx file is referred and appropriate language text is shown.

    So this can be achieved with the following line of code in .Net aspx page,

    protected override void InitializeCulture()
    {

       CultureInfo cultureInfo = null;
       if (Request.UserLanguages == null)  return;
       if (Request.UserLanguages.GetLength(0) <1)  return;

       foreach(string userLanguage in Request.UserLanguages)
       {
      
         cultureInfo = GetCultureInfo(userLanguage,true);
            
         if (cultureInfo != null)
         {
           Thread.CurrentThread.CurrentUICulture = cultureInfo;
           Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
           break;
         }
       }
      
    }

    protected virtual CultureInfo GetCultureInfo(string userLanguage,bool useUserOverride)
    {
          int semiColonIndex = userLanguage.IndexOf(";");

          try
          {

             if (semiColonIndex != -1)
             {
                  userLanguage  = userLanguage.Substring(0,semiColonIndex);
             }

              return new CultureInfo(userLanguage,useUserOverride);

           }
          catch (ArgumentException) {  return null;  }
              
    }

    but here in umbraco we dont have the aspx page. And how would I set at runtime to the usercontrol, so that the appropriate resx(resource)  file is used. Also I would need to know how these .resx files are deployed in umbraco?

    i'm new to umbraco, any help would be appreciated.

    Thanks in Advance,

    Regards

    Santhosh

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Feb 17, 2011 @ 13:26
    Michael Latouche
    0

    Hi Santhosh,

    If you want to use the .Net resources, you can also set the culture in the global.asax file, in the Application_BeginRequest method.

    Cheers,

    Michael.

  • Santhosh 58 posts 99 karma points
    Feb 24, 2011 @ 11:55
    Santhosh
    0

    I found the answer here from the below video from Umbraco. Thanks Everyone for the Inputs. It was quite simple to achieve Globalization/Localization using .Net User controls + Resource Files(.resx)

    Please Look at the below video (Pls look till the end).
    http://umbraco.com/help-and-support/video-tutorials/introduction-to-umbraco/developer-introduction/using-net-user-controls

Please Sign in or register to post replies

Write your reply to:

Draft