Automatic match the language in nop to the visitors Browser set languages

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Hi,

I have a site in the Netherlands "nl-NL" and visitors are also from foreign countries.

I want my visitors to get the language "en" and visitors from Belgium "nl-BE" and "nl-NL" the language "nl". You can't do this default in nop.

With this MOD you only have to install two languages "en-US" and "nl-NL" and the rest is done automaticly.

Feel free to use this in the newest version.

In nop v1.80
In ..\Libraries\NopContext.cs
Find this:

        public Language WorkingLanguage
        {
            get
            {
                var customer = NopContext.Current.User;
                var publishedLanguages = LanguageManager.GetAllLanguages(false);
                if (customer != null)
                {
                    var customerLanguage = customer.Language;
                    if (customerLanguage != null && customerLanguage.Published)
                        return customerLanguage;
                }
                else if (CommonHelper.GetCookieInt("Nop.CustomerLanguage") > 0)
                {
                    var customerLanguage = LanguageManager.GetLanguageById(CommonHelper.GetCookieInt("Nop.CustomerLanguage"));
                    if (customerLanguage != null)
                        if (customerLanguage != null && customerLanguage.Published)
                            return customerLanguage;
                }

                foreach (var _language in publishedLanguages)
                    return _language;


And change it to his:

        public Language WorkingLanguage
        {
            get
            {
                var customer = NopContext.Current.User;
                var publishedLanguages = LanguageManager.GetAllLanguages(false);
                if (customer != null)
                {
                    var customerLanguage = customer.Language;
                    if (customerLanguage != null && customerLanguage.Published)
                        return customerLanguage;
                }
                else if (CommonHelper.GetCookieInt("Nop.CustomerLanguage") > 0)
                {
                    var customerLanguage = LanguageManager.GetLanguageById(CommonHelper.GetCookieInt("Nop.CustomerLanguage"));
                    if (customerLanguage != null)
                        if (customerLanguage != null && customerLanguage.Published)
                            return customerLanguage;
                }

                //MOD_MATCHLANGUAGE>
                //Gets all preferred languages from the browser client
                //and tries to match this with a published nop language
                string[] languages = HttpContext.Current.Request.UserLanguages;
                if ((languages != null) && (languages.Length > 0))
                {
                    //size=5, First time we look for an exact match "en-US", "nl-NL", etc.
                    //size=2, Second time we look for the first match on "en", "nl", etc.
                    for (int size = 5; size >= 2; size=size-3)
                    {
                        foreach (string language in languages)
                        {
                            string sLanguageBrowser = language.ToLowerInvariant().Trim();
                            if (sLanguageBrowser.Length >= size)
                            {
                                sLanguageBrowser = sLanguageBrowser.Substring(0, size);
                                foreach (var _language in publishedLanguages)
                                {
                                    string sLanguagePublished = _language.LanguageCulture.ToLowerInvariant().Trim();
                                    if (sLanguagePublished.Length >= size)
                                    {
                                        sLanguagePublished = sLanguagePublished.Substring(0, size);

                                        //compare first "en-US" and second time "en"
                                        if (sLanguageBrowser == sLanguagePublished)
                                        {
                                            return _language;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //MOD_MATCHLANGUAGE<


                foreach (var _language in publishedLanguages)
                    return _language;

13 years ago
Hey ajhvdb, this is a great piece of code! I've just put it to my store, but in my case I've done one change: I swapped the two foreach statements, because I prefer to give priority to the base language instead of an exact match, that is, I prefer that two flavors of Spanish (i.e. es-AR and es-ES) match as Spanish rather than exactly matching en-GB, for example.

foreach (string language in languages)
         {
              string sLanguageBrowser = language.ToLowerInvariant().Trim();
              
              for (int size = 5; size >= 2; size=size-3)
              {    
                        


Thanks!

Francesc
12 years ago
Can not find directory \Libraries in my installation neither the module NopContext.cs.

Anybody to help me

Thanks in advance

Michael
12 years ago
The example code is written for 1.90 not for 2.00.
12 years ago
HI Marcel,

My installed version is 1.90

Thanks
Michael
12 years ago
It's in nopCommerce-V1.9\Libraries\Nop.BusinessLogic\NopContext.cs

The code is almost the same:


        /// <summary>
        /// Get or set current user working language
        /// </summary>
        public Language WorkingLanguage
        {
            get
            {

                //MOD_MATCHLANGUAGE>
                //tries to match querystring language=? with a published nop language
                string sRequestLanguage = HttpContext.Current.Request.QueryString["language"];
                //size=5, First time we look for an exact match "en-US", "nl-NL", etc.
                //size=2, Second time we look for the first match on "en", "nl", etc.
                if (!string.IsNullOrEmpty(sRequestLanguage))
                {
                    for (int size = 5; size >= 2; size = size - 3)
                    {
                        string sLanguage = sRequestLanguage.ToLowerInvariant().Trim();
                        if (sLanguage.Length >= size)
                        {
                            sLanguage = sLanguage.Substring(0, size);
                            var pubLanguages = IoC.Resolve<ILanguageService>().GetAllLanguages(false);

                            foreach (var vlanguage in pubLanguages)
                            {
                                string sLanguagePublished = vlanguage.LanguageCulture.ToLowerInvariant().Trim();
                                if (sLanguagePublished.Length >= size)
                                {
                                    sLanguagePublished = sLanguagePublished.Substring(0, size);

                                    //compare first "en-US" and second time "en"
                                    if (sLanguage == sLanguagePublished)
                                    {
                                        this._workingLanguage = vlanguage;
                                        return this._workingLanguage;
                                    }
                                }
                            }
                        }
                    }
                }
                //MOD_MATCHLANGUAGE<

                //cached value
                if (this._workingLanguage != null)
                    return this._workingLanguage;

12 years ago
Hi,

The installed version is 1.90 but the folderLLibraries you mention does not exist!
So there is not any file no change

Thanks
Michael
12 years ago
As you can see the file exist on my PC, it's located in ..\nopCommerce-V1.9\Libraries\Nop.BusinessLogic\NopContext.cs

Did you download the full version (with source code)
12 years ago
Hi,

no i downloaded the version without source code. I will do. But how to add now
this or probably others folders in the existing one succesfully?
12 years ago
For nopCommerce v2.10 the location is;
/Libraries/Nop.BusinessLogic/NopContext.cs
(from line 400)

Original code;
        
public Language WorkingLanguage
        {
            get
            {
                //cached value
                if (this._workingLanguage != null)
                    return this._workingLanguage;

                //customer language
                if (this.User != null)
                {
                    var customerLanguage = this.User.Language;
                    if (customerLanguage != null && customerLanguage.Published)
                    {
                        this._workingLanguage = customerLanguage;
                        return this._workingLanguage;
                    }
                }
                else if (CommonHelper.GetCookieInt("Nop.CustomerLanguage") > 0)
                {
                    var customerLanguage = IoC.Resolve<ILanguageService>().GetLanguageById(CommonHelper.GetCookieInt("Nop.CustomerLanguage"));
                    if (customerLanguage != null && customerLanguage.Published)
                    {
                        this._workingLanguage = customerLanguage;
                        return this._workingLanguage;
                    }
                }
                var publishedLanguages = IoC.Resolve<ILanguageService>().GetAllLanguages(false);
                foreach (var language in publishedLanguages)
                {
                    this._workingLanguage = language;
                    return this._workingLanguage;
                }

                throw new NopException("Languages could not be loaded");
            }


Modified code;
    
public Language WorkingLanguage
        {
            get
            {
                //cached value
                if (this._workingLanguage != null)
                    return this._workingLanguage;

                //customer language
                if (this.User != null)
                {
                    var customerLanguage = this.User.Language;
                    if (customerLanguage != null && customerLanguage.Published)
                    {
                        this._workingLanguage = customerLanguage;
                        return this._workingLanguage;
                    }
                }
                else if (CommonHelper.GetCookieInt("Nop.CustomerLanguage") > 0)
                {
                    var customerLanguage = IoC.Resolve<ILanguageService>().GetLanguageById(CommonHelper.GetCookieInt("Nop.CustomerLanguage"));
                    if (customerLanguage != null && customerLanguage.Published)
                    {
                        this._workingLanguage = customerLanguage;
                        return this._workingLanguage;
                    }
                }
                var publishedLanguages = IoC.Resolve<ILanguageService>().GetAllLanguages(false);

                //MOD_MATCHLANGUAGE>
                //Gets all preferred languages from the browser client
                //and tries to match this with a published nop language
                string[] languages = HttpContext.Current.Request.UserLanguages;
                if ((languages != null) && (languages.Length > 0))
                {
                  //size=5, First time we look for an exact match "en-US", "nl-NL", etc.
                  //size=2, Second time we look for the first match on "en", "nl", etc.
                  for (int size = 5; size >= 2; size = size - 3)
                  {
                    foreach (string language in languages)
                    {
                      string sLanguageBrowser = language.ToLowerInvariant().Trim();
                      if (sLanguageBrowser.Length >= size)
                      {
                        sLanguageBrowser = sLanguageBrowser.Substring(0, size);
                        foreach (var _language in publishedLanguages)
                        {
                          string sLanguagePublished = _language.LanguageCulture.ToLowerInvariant().Trim();
                          if (sLanguagePublished.Length >= size)
                          {
                            sLanguagePublished = sLanguagePublished.Substring(0, size);
                            //compare first "en-US" and second time "en"
                            if (sLanguageBrowser == sLanguagePublished)
                            {
                              return _language;
                            }
                          }
                        }
                      }
                    }
                  }
                }
                //MOD_MATCHLANGUAGE<

                foreach (var language in publishedLanguages)
                {
                    this._workingLanguage = language;
                    return this._workingLanguage;
                }

                throw new NopException("Languages could not be loaded");
            }


Thanks for this nice add-on!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.