Localize XML data files?

Last post 05-26-2007 1:08 PM by cml. 18 replies.

Sort Posts:

  • Localize XML data files?

    03-05-2007, 7:31 AM
    • Loading...
    • cml
    • Joined on 03-05-2007, 7:25 AM
    • Posts 10

    Hi all!

    I am trying to localize my small business starter kit. I have added resource files for all static content and that works fine. I have added in functionality to change the language by clicking buttons, but I now need to find a way of selecting different XMl data files for different languages.

    I have added in a new file name to the web.config in the datafilename key, so that I can pick up one or the other just by the name of it. BUT, in the code I need to get to the preferred language to make the choice and I am not sure how to do this. I am setting a session variable with teh language but can't get to it from the cs file...
     
    Is there a way of setting up a global kind of variable or something so that I can check that when selecting which xml file to read? All my page are based on a PageBase class where I check the culture and set the session language. Can I somehow add in somethign here that I can interrogate from the code later to choose the correct XML file?

    Would really appreciate help!

    Cheers,

    Carin 

    Filed under: , ,
  • Re: Localize XML data files?

    03-05-2007, 9:40 AM

    Well there is something better than a global variable, it's called the Profile in ASP.Net 2.0

    <

    profile>

    <

    properties>

    <

    add name="PreferredLocale" allowAnonymous="true" defaultValue="en-US"></add>

     

    </

    properties>

    </

    profile>

     U can call the Profile api from anywhere in ur pages.

    Profile.PreferredLocale

    when u click on a specific language button, just change the preferredlocal language

    Here's a small article that explains the whole issue.

    http://www.odetocode.com/Articles/440.aspx

    Regards
    Ramzi
    --------------------------------------------
    Dont Forget to mark the post as answered once replied.
  • Re: Localize XML data files?

    03-05-2007, 9:47 AM
    • Loading...
    • cml
    • Joined on 03-05-2007, 7:25 AM
    • Posts 10

    Thanks Ramzi.

    OK, but can you get to the Profile even in a class file? My problem was that I couldn't access the Session info from the class that gets the name of the XML file from web.config, can't remember which .cs it was...

    Cheers,

    Carin
     

  • Re: Localize XML data files?

    03-05-2007, 2:21 PM

    yes u can , here's how

    HttpContext

    .Current.Profile["PreferredLocale"]
    Regards
    Ramzi
    --------------------------------------------
    Dont Forget to mark the post as answered once replied.
  • Re: Localize XML data files?

    03-05-2007, 2:49 PM
    • Loading...
    • cml
    • Joined on 03-05-2007, 7:25 AM
    • Posts 10

    Ramzi,

    Thanks for helping. I am still having problems though... Trying to just implement the simple example from that article you gave the link for: I added in the relevant bits to my web.config:

        <system.web>
            <customErrors mode="RemoteOnly" defaultRedirect="CustomError.aspx"/>
            <pages theme="Default"/>
            <compilation debug="true"/>
        <globalization  fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8"  />
        <anonymousIdentification enabled="true"/>
        <trust level="Medium"/>
        <profile >
          <properties>
            <add name="Name" allowAnonymous="true" />
            <add name="Age" allowAnonymous="true" type="System.Int16"/>
          </properties>
        </profile>

      </system.web>

    then tried it out by adding in a test page with that small simple example and got this error message:

    The SSE Provider did not find the database file specified in the connection string. At the configured trust level (below High trust level), the SSE provider can not automatically create the database file.

    What I don't understand is that is says the simple example is supposed to work wihtout a DB, and I don't want to store this info away, it's only going to be for the session really. So I am wondering whether a session variable isn't a better way of doing it and then using a global variable or something. They actually say that in the feedback responses of that post.

    However, I still don't understand why that simple example doesn't work. Do I have to store the profile in DB? I mean I can see why you would want to for other stuff, but it's not what I'm after.

    I would appreciate if you could point me in the right direction of maybe using a variable somehow? 

    Thanks again for your help!
    Cheers,

    Carin
     

  • Re: Localize XML data files?

    03-05-2007, 3:33 PM

    well in order to use profile, u have to implement the ASPNet DB

    c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe

    If u dont like this solution. well u have to stick to Session, cause its the only one thats unique for each user.

    Inform me if u need help in a specific topic, ill be glad to provide it Carin Wink

    Regards
    Ramzi
    --------------------------------------------
    Dont Forget to mark the post as answered once replied.
  • Re: Localize XML data files?

    03-05-2007, 4:00 PM
    • Loading...
    • cml
    • Joined on 03-05-2007, 7:25 AM
    • Posts 10

    Ramiz,

    Yes, this it it, I want to use only XML for data storage in my small business site starter kit, so don't want to store using SQL and am not interested in storing this for future visits either. The language is detected using the browser setup but I also want to give the user the ability to change language.

    I currently have a PageBase.cs that all pages inherit from where I check for postbacks when user has clicked a button to change language. Default (from browser) or selected language is set there and stored in a session variable.

    Can I somehow create a variable that I can access from the app_code .cs code files? The session is out of scope there. I cannot e.g. use System.Web.SessionState.HttpSessionState["thesessionvariablename"].ToString() from here. Or am I missing something?


    Cheers,
    Carin

  • Re: Localize XML data files?

    03-05-2007, 4:19 PM

    True u can use session state as stated above and also like this

    HttpContext.Current.Session["PreferredLocale"]

    as for the language button event include the following to change language

    string _Lang = "en-US";
    if (HttpContext.Current.Session["PreferredLocale"] != null)
    {

        _Lang =

    HttpContext.Current.Session["PreferredLocale"].ToString();
    }
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(_Lang);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(_Lang);

    Hope this is what ur looking for.

    Cheers

    Regards
    Ramzi
    --------------------------------------------
    Dont Forget to mark the post as answered once replied.
  • Re: Localize XML data files?

    03-05-2007, 4:43 PM
    • Loading...
    • cml
    • Joined on 03-05-2007, 7:25 AM
    • Posts 10

    No, that bit I've got sorted, it's when I need to choose what xml file to read in XmlCatalogProvider.cs that I need to access the language pref so that I can select the XML with the correct language. I have created copies of the items.xml and translated them and added in this to the web.config.

            <CatalogProviders>
                <add name="sqlProvider" type="SqlCatalogProvider" connectionStringName="SQLConnectionString"/>
                <add name="xmlProvider" type="XmlCatalogProvider" schemaFile="Items.xsd" dataFile="Items.xml" dataFile_SE="Items_SE.xml"/>
            </CatalogProviders> 

    So, in XmlCatalogProvider.cs I want to check language so that I can surround the following with some if statments to pick the right file from web.config.

    string xmlFile = sec.CatalogProviders[sec.CatalogProviderName].Parameters["dataFile"];

    Hope that makes sense! 

    Cheers,

    Carin
     

  • Re: Localize XML data files?

    03-06-2007, 1:34 AM

    I makes perfect sense Smile

    waht u need to do is place different language tags in ur XML, then use a method to read the correct language from ur xml according to the language saved in ur session.

    Hope this is what u meant.

    Regards
    Ramzi
    --------------------------------------------
    Dont Forget to mark the post as answered once replied.
  • Re: Localize XML data files?

    03-06-2007, 7:43 AM
    • Loading...
    • cml
    • Joined on 03-05-2007, 7:25 AM
    • Posts 10

    Ramzi,

    Thanks for reply again!

    Yes, I see what you mean, but how do I actually do this?? I am not sure where to write that function and what it needs to look like... Could you maybe give me some pointers to get me started?...

    As I'm sure you can tell I am pretty much a beginner at these things! :) 

    Cheers,
    Carin
     

  • Re: Localize XML data files?

    03-06-2007, 8:18 AM

    Everyone have a weak point in something, its ok Wink

    Ur function will look something like this

    if ur XML tag is like so, <data><en-US>English text</en-US><fr-FR>french text</fr-FR></data>

    where language will be the culture --> en-US, orfr-FR etc ...

    public static string GetTextByCurrentLocale(string data,string language)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(data);
                    XmlNode node = doc.SelectSingleNode("data/" + language);
                    if (node == null)
                    {
                        return "";
                    }
                    else
                    {
                        return node.InnerText;
                    }
                }
                catch { return data; }
            }
     Hope this is what ur looking for Caren

     

    Regards
    Ramzi
    --------------------------------------------
    Dont Forget to mark the post as answered once replied.
  • Re: Localize XML data files?

    03-07-2007, 4:37 AM
    • Loading...
    • cml
    • Joined on 03-05-2007, 7:25 AM
    • Posts 10

    Hi Ramzi,

    Just to say thanks for trying to help me with my problems with the localization!

    I actually solved it yesterday and I am really embarrassed to say that I had just not used the correct namespace when trying to access the session variables from the code... So I used the solution I had attempted from the start and now I've got it working reading in different data files for different languages.

    So, once again, thanks and sorry if I wasted your time! But, I have to say that I learnt quite a few things about postbacks and stuff, so it wasn't wasted as far as I'm concerned! :)

    Cheers,

    Carin
     

  • Re: Localize XML data files?

    03-07-2007, 4:56 AM

    Well Caren

    U didnt waste my time at all, on the contrary, i got to share nice stuff with u, (cocing stuff , but still nice Smile)

    Glad it worked out fine for you.

    If u need any other help in what so ever, ill be ready to provide it.

     

    Regards
    Ramzi
    --------------------------------------------
    Dont Forget to mark the post as answered once replied.
  • Re: Localize XML data files?

    03-28-2007, 12:55 PM
    • Loading...
    • cml
    • Joined on 03-05-2007, 7:25 AM
    • Posts 10

    I'm back! 

    I have just managed to get back on to my small business site again.... I managed to solve the localization of the XML data files and have now moved on to the sitemap.

    Have changed the sitemap to use: "$Resources:Navigation,Homepage" format for the text displayed and also created a global resource folder with the relevant files needed.

    Now, this doesn't work when I use my language selection buttons, which are the ones you helped me with before and where I set the culture at postback/click. I assume the problem is that the app doesn't reload the sitemap with the different language, it stays the same. I shoudl say that everythign else on my page does display the correct language so it's not the localization itself not working.

    AND, when I go to a different page, it does change to the correct langage (that I selected using the buttons in previous page) in the navigation display.

    SO, how do I get the app to "re-load" the sitemap somehow to get the new language through?

    Cheers,

    Carin
     

Page 1 of 2 (19 items) 1 2 Next >