change language by visitor IP

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 anos atrás
I can detect the visitor's country code by using the latest geoip.dat file.

How can I change the public store's language by using this information for the visitor?
11 anos atrás
Here is my solution to my own problem, may be someone else needs it also:

just add this code to global.asax.cs file


protected void Session_Start(object sender, EventArgs e)
        {
            var _workContext = EngineContext.Current.Resolve<IWorkContext>();
            var _webHelper = EngineContext.Current.Resolve<IWebHelper>();
            var _languageService = EngineContext.Current.Resolve<ILanguageService>();

            Nop.Services.Directory.GeoCountryLookup geo = new Nop.Services.Directory.GeoCountryLookup(_webHelper);
            string country = geo.LookupCountryCode(Request.ServerVariables["REMOTE_ADDR"]);
            if (country.Equals("TR"))
            {
                var language = _languageService.GetLanguageById(0);
                _workContext.WorkingLanguage = language;
            }
            else
            {
                var language = _languageService.GetLanguageById(1);
                _workContext.WorkingLanguage = language;
            }
        }
11 anos atrás
I would personally do it in CustomerService::InsertGuestCustomer() in Libraries\Nop.Services\Customers\CustomerService.cs. And based on the country I would specify the correct language for the new guest customer being created.

If you do it in Session_Start(), your logic will also be applied on logged-in users whose language preferences may not coincide with your logic.

In InsertGuestCustomer():

            var customer = new Customer()
            {
                CustomerGuid = Guid.NewGuid(),
                Active = true,
                CreatedOnUtc = DateTime.UtcNow,
                LastActivityDateUtc = DateTime.UtcNow,
// Specify the language based on the client IP:
                Language = ................. // Insert your logic here
            };
11 anos atrás
Thanks for the info. I'm new to nopcommerce and don't know the architect well for now.

For a quick hack, I put


if (String.IsNullOrEmpty(_workContext.CurrentCustomer.Username))


control to my code to check if the session user is guest. I'll study that portion of the code you show later.

Thanks again..
7 anos atrás
rasel wrote:
Here is my solution to my own problem, may be someone else needs it also:

just add this code to global.asax.cs file


protected void Session_Start(object sender, EventArgs e)
        {
            var _workContext = EngineContext.Current.Resolve<IWorkContext>();
            var _webHelper = EngineContext.Current.Resolve<IWebHelper>();
            var _languageService = EngineContext.Current.Resolve<ILanguageService>();

            Nop.Services.Directory.GeoCountryLookup geo = new Nop.Services.Directory.GeoCountryLookup(_webHelper);
            string country = geo.LookupCountryCode(Request.ServerVariables["REMOTE_ADDR"]);
            if (country.Equals("TR"))
            {
                var language = _languageService.GetLanguageById(0);
                _workContext.WorkingLanguage = language;
            }
            else
            {
                var language = _languageService.GetLanguageById(1);
                _workContext.WorkingLanguage = language;
            }
        }




I need to do this through plugin. I dont want to change anything into nopCommerce code. so how can i do this?
7 anos atrás
Hi,

once you create the plugin, override the method InsertGuestCustomer().

I think it will work.

Thanks
7 anos atrás
Hi,

I am trying to use the same code for geolocation redirect. I am using NOP 3.80 I am getting the error at

  Nop.Services.Directory.GeoLookupService geo = new Nop.Services.Directory.GeoLookupService(_webHelper);

Error is "can not convert from Nop.Core.IWebHelper to Nop.Services.Logging.ILogger" Please help
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.