Copied to clipboard

Flag this post as spam?

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


  • Saied 349 posts 674 karma points
    Feb 03, 2016 @ 22:05
    Saied
    0

    Pass culture to GetDictionaryValue

    I have english and spanish setup on my website.

    When calling @umbraco.GetDictionaryValue, is it possible to pass the culture or do I have to create different domains. I currently have sctflash.com, but I don't want to have to createsctflash.com/en, sctflash.com/es, etc. What are my options?

    I tried changing my browser language, but that had no effect.

  • Daniel 60 posts 174 karma points
    Feb 03, 2016 @ 22:14
    Daniel
    1

    You can do something like:

    var service = ApplicationContext.Current.Services.LocalizationService;
    var dictionaryItem = service.GetDictionaryItemByKey(dictionaryKey);
    var translation = dictionaryItem.Translations.SingleOrDefault(x => x.Language.Id.Equals(languageId));
    return translation.Value;
    

    You can probably make it prettier, this code snippet is somewhat old!


    Extra (useless info):

    The reason I personally used the "language.Id" to match, was because I was using a language selector based on the avaliable languages in the language settings - something like:

    @foreach (var language in UmbracoContext.Application.Services.LocalizationService.GetAllLanguages())
    {
        <li class="flag-item">
            <a class="flag @language.CultureInfo.TwoLetterISOLanguageName" href="@Url.Action("ChangeLanguage","LanguageSurface", new{ language.Id })"></a>
        </li>
    }
    
  • Saied 349 posts 674 karma points
    Feb 03, 2016 @ 23:36
    Saied
    0

    HI Daniel,

    Where would I put this code:

    var service = ApplicationContext.Current.Services.LocalizationService;
    var dictionaryItem = service.GetDictionaryItemByKey(dictionaryKey);
    var translation = dictionaryItem.Translations.SingleOrDefault(x => x.Language.Id.Equals(languageId));
    return translation.Value;
    

    and

    Can you explain some of it?

    I am probably gonna do a language selector too. Have you found that easier than relying on the browser only?

    Also, if I pick want to do spanish, do I have to create one for every variation of spanish (Us-es, es, es-ES, etc)? That seems like a pain.

    Thanks for the wonderful help, Saied

  • Daniel 60 posts 174 karma points
    Feb 04, 2016 @ 01:06
    Daniel
    0

    Sure!

    You could do a class like:

    public static class MyDictionary
    {
        public String GetTranslationForLanguage(String dictionaryKey, int languageId) {
            var service = ApplicationContext.Current.Services.LocalizationService; //just to point out the service that governs the dictionary/localization
            var dictionaryItem = service.GetDictionaryItemByKey(dictionaryKey); //target the dictionary item found in Umbraco/Settings in the backoffice
            var translation = dictionaryItem.Translations.SingleOrDefault(x => x.Language.Id.Equals(languageId)); //choose the translation of the dictionary item by language ID
            return translation.Value; //return the value of the translation of the selected language
        }
    }
    

    You could find the translation by any .Equals() or == function in the SingleOrDefault(), I just chose to match an ID because that matched what I had to search with.

    There are a few methods to match the language you need, you could also do something like:

    var translation = dictionaryItem.Translations.SingleOrDefault(x => x.Language.CultureName.Equals(....));
    

    You could try making a dot after the language property to check which match would suit your code best!

    I'm not sure if you're selecting the language on the website (like a flag picker) or you could get it off something like:

    CultureInfo.CurrentCulture
    

    If you did it based on id, you could then call the first static method like:

    var dictionaryKey = "exampleKey"; //the name of the dictionary key in the Umbraco Settings tab
    var firstLanguage = UmbracoContext.Application.Services.LocalizationService.GetAllLanguages().First(); //Just taking the first language in the list from the Umbraco Settings tabs under the Language node as example
    var translatedText = MyDictionary.GetTranslationForLanguage(dictionaryKey, firstLanguage.Id) //now you have the translated value!
    

    You could probably even match the language from .GetAllLanguages() with something like:

    var languageIdentifier = CultureInfo.CurrentCulture.? //some property here
    var allLanguages = UmbracoContext.Application.Services.LocalizationService.GetAllLanguages();
    var currentLanguage = allLanguages.SingleOrDefault(...) //match inside the SingleOrDefault based on your languageIdentifier
    

    Regarding the language selector, I'd definitely recommend it, always nice to give the poor users the choice if they end up in an unwanted language!

    I think one of each variety for Spanish would be a bit overkill, "es-ES" is usually good, but of course it depends if your website is about Spanish talking countries or has the specific need to divide the language, I haven't seen that need a lot though!


    Regarding the language picker:

    The reason I foreach'ed over the language ids in my first post, is because when they click a flag and send the language id to my Controller, I put that id into a session variable like:

    Session["lang"] = id;
    

    If you ensure this is always set (or have some null checks and possibly a default language), you can skip sending the id into the "MyDictionary.GetTranslationForLanguage()" method and take the language id from the session, and only send in the key, but that's just one solution!

  • Saied 349 posts 674 karma points
    Feb 04, 2016 @ 02:00
    Saied
    0

    Daniel,

    Thanks for the great info.

    1. Have you had any issues using Session, since it is created on the server?
    2. When the user selects their language from the selector, is this when you set the id of the language in session or some cookie for example?

    .

  • Daniel 60 posts 174 karma points
    Feb 04, 2016 @ 08:50
    Daniel
    0

    I had a few issues while testing, but after ensuring null checks on the session + selecting a fallback language if no session is found (English in my case) - it's been working flawlessly since then!

    In my specific case, I had a list of links to a Controller method like:

    <a class="flag @language.CultureInfo.TwoLetterISOLanguageName" href="@Url.Action("ChangeLanguage","LanguageSurface", new{ language.Id })"></a>
    

    Styling the flag icon something like:

    .flag.en { ... }
    .flag.de { ... }
    

    Then when they clicked the a element - which probably should be a button element - I'd set the session in the Controller and return a redirect, something like:

    public ActionResult ChangeLanguage(int id)
    {
        Session["lang"] = id;
        //some short code to figure out where to redirect the user / maybe just to the same page
        return Redirect(url);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft