Copied to clipboard

Flag this post as spam?

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


  • Colin 46 posts 146 karma points
    Mar 02, 2020 @ 18:17
    Colin
    0

    How to retrieve data for country

    Hi

    I have two international sites and have set the culture, hostname.

    I am storing some data in a database. I know how to retrieve data but how could I ensure I get the data for the correct country?

    Thanks

  • Colin 46 posts 146 karma points
    Mar 03, 2020 @ 17:12
    Colin
    0

    Anyone got any thoughts or ideas?

    Thanks

  • Mila Pandurska 75 posts 353 karma points
    Mar 04, 2020 @ 07:24
    Mila Pandurska
    1

    Hi Colin, What do you mean by storing some data in the database? Do you mean some custom tables or Umbraco content? Can you describe in more details the question?

  • Colin 46 posts 146 karma points
    Mar 04, 2020 @ 14:26
    Colin
    0

    Hi Mila

    By that I mean for Germany I have a custom table which stores a record for them in German. I'm also storing the language/country against that record.

    On the other hand I am storing data for USA in English (again storing the language/country) against that record.

    So if I'm a user from Germany I would like to display the record in German. How could I ensure the data displayed for the German user is in German and USA user is in English?

    I have setup the global resources folder with minor translations. So it's a matter of working out how to display the appropriate record for the user of that country.

    Let me know if you require more info. Thanks

  • Mila Pandurska 75 posts 353 karma points
    Mar 04, 2020 @ 14:29
    Mila Pandurska
    1

    Hi Colin, You can get current language (user's language) and pass this as parameter to your query and get the data from the custom table.

    Does it makes sence?

    Mila

  • Colin 46 posts 146 karma points
    Mar 04, 2020 @ 19:13
    Colin
    0

    It does but how do I do that? I've read on cultureInfo class but not sure if this is the correct way? Thanks

  • Mila Pandurska 75 posts 353 karma points
    Mar 05, 2020 @ 07:04
    Mila Pandurska
    1

    Hi Colin, Yes, you can get the culture in your controllers with the following line:

     var culture = CultureInfo.CurrentCulture;
    

    Keep in mind that if you use Ajax requests this code will not work. In case you use Ajax you need to get the current Culture from the Umbraco view (using the same code above) and pass it as parameter in your ajax GET/POST request. Ajax requests doesn't know anything about Umbraco context and culture.

    There is another option: Create a default cotrnoller where you can get the current culture and set a cookie. After that you can work only with your cookie. Here you can find information how to create default controller. If you need I can share some code about this approach.

    Hope that this will help Mila

  • Colin 46 posts 146 karma points
    Mar 05, 2020 @ 07:49
    Colin
    0

    Hi Mila, thanks for that I will check it out.

    Just to ensure there's no twist or turns, I am using a web control. This control has a public property for the culture where I was planning on adding the country/language.

    Using your method I won't need to do that I assume? If that's the case could I use one web control/macro for multiple sites by just adding the culture code you provided above which will then get data by country and for any new countries I add in future?

    Thanks again

  • Mila Pandurska 75 posts 353 karma points
    Mar 05, 2020 @ 08:23
    Mila Pandurska
    0

    Hi Colin,

    As long as you are in Umbraco context you can always get the current culture. It is not needed to pass it as additional parameter. I am not very sure how your macro works so if you want you can post some details here.

    Mila

  • Colin 46 posts 146 karma points
    Mar 05, 2020 @ 12:57
    Colin
    0

    Hi Mika

    I appreciate your thoughts and help. Currently im storing records as below (I've cut this down dramatically)

    country - languageCode - name USA - en-US - Hello Canada - ca-En - Hi Canada -ca-Fr - Bonjour UK - en-GB - Hiya Germany - de-AT - Guten tag

    I will give what you have recommended a go and see how i go. Once i know i dont have any issues i will mark your reply as an answer.

    Thanks

  • Colin 46 posts 146 karma points
    Mar 06, 2020 @ 20:46
    Colin
    0

    Hi

    I did a quick test on the above but that code always returns the culture that's on the server/pc and not the browser? Anything I need to do?

    Thanks

  • Mila Pandurska 75 posts 353 karma points
    Mar 07, 2020 @ 08:37
    Mila Pandurska
    100

    Hi Colin, Did you set correct the culture and hostnames of your home node. And can you give more details where exactly you try to get culture - in your controller, view... A bit more information about the setup you have.

  • Colin 46 posts 146 karma points
    Mar 12, 2020 @ 06:53
    Colin
    0

    Hi Mila

    So I have a web forms control with only the above culture code on page load (so there is no inheritance or anything as such.

    I have set the culture under hostnames for each country.

    I'm not setting any cookies. To test, I add the language to the browser settings and make that the default/highest priority.

    I changed the HTML template and added the language to it which showed the correct data. I'm testing this as I write to see if this works under all browsers/scenarios.

    Finally if a user is in Germany I would like to give them the option to change to UK. I thought to add this in in case it changes anything.

    Many thanks for your help.

Please Sign in or register to post replies

Write your reply to:

Draft