Post by Hourglasss » Tue Mar 21, 2023 10:42 pm

I have 2 langauges on my webshop dutch and english how to make it so it auto changes the language so if the user is from a Dutch IP that the language is set to dutch and any other country it using english right now ive set dutch as Default but im pretty sure it automaticly loads english, im using 3.0.3.8 and using a free Dutch translation package, i have cloudflare but i already using the mod to resolve the customers real IP does open cart 4.0.0.0 does this automaticly? i saw threads thats en=US or something is parsed to the URL

Active Member

Posts

Joined
Mon Jan 23, 2023 10:39 pm

Post by by mona » Tue Mar 21, 2023 11:51 pm


DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by Hourglasss » Wed Mar 22, 2023 3:34 am

Shouldnt opencart do this already?

Code: Select all

	// Language
		$code = '';
		
		$this->load->model('localisation/language');
		
		$languages = $this->model_localisation_language->getLanguages();
		
		if (isset($this->session->data['language'])) {
			$code = $this->session->data['language'];
		}
				
		if (isset($this->request->cookie['language']) && !array_key_exists($code, $languages)) {
			$code = $this->request->cookie['language'];
		}
		
		// Language Detection
		if (!empty($this->request->server['HTTP_ACCEPT_LANGUAGE']) && !array_key_exists($code, $languages)) {
			$detect = '';
			
			$browser_languages = explode(',', $this->request->server['HTTP_ACCEPT_LANGUAGE']);
			
			// Try using local to detect the language
			foreach ($browser_languages as $browser_language) {
				foreach ($languages as $key => $value) {
					if ($value['status']) {
						$locale = explode(',', $value['locale']);
						
						if (in_array($browser_language, $locale)) {
							$detect = $key;
							break 2;
						}
					}
				}	
			}			
			
			if (!$detect) { 
				// Try using language folder to detect the language
				foreach ($browser_languages as $browser_language) {
					if (array_key_exists(strtolower($browser_language), $languages)) {
						$detect = strtolower($browser_language);
						
						break;
					}
				}
			}
			
			$code = $detect ? $detect : '';
		}
		
		if (!array_key_exists($code, $languages)) {
			$code = $this->config->get('config_language');
		}
		
		if (!isset($this->session->data['language']) || $this->session->data['language'] != $code) {
			$this->session->data['language'] = $code;
		}
				
		if (!isset($this->request->cookie['language']) || $this->request->cookie['language'] != $code) {
			setcookie('language', $code, time() + 60 * 60 * 24 * 30, '/', $this->request->server['HTTP_HOST']);
		}

Active Member

Posts

Joined
Mon Jan 23, 2023 10:39 pm

Post by by mona » Wed Mar 22, 2023 4:49 am

browser language and ip location are two different things
viewtopic.php?t=221079#p805659
viewtopic.php?t=206944

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by Hourglasss » Wed Mar 22, 2023 6:15 am

by mona wrote:
Wed Mar 22, 2023 4:49 am
browser language and ip location are two different things
viewtopic.php?t=221079#p805659
viewtopic.php?t=206944
ok let me rephrase my question then how can i just make dutch my primary langauge no mather what,
ii just need to load my website in dutch no mather browser language and have english as an option peopel can switch to if they dont speak dutch

Active Member

Posts

Joined
Mon Jan 23, 2023 10:39 pm

Post by by mona » Wed Mar 22, 2023 6:24 am

It should do that if the default is set to dutch, as an additional safeguard put sort order as dutch 1 and english 2
You can check by clearing your browser cookies or post your webpage and someone can check.

If you are not sure how to clear your cookies (and ideally you just want to your site cookies specifically) you can search google, these used to be correct.
https://support.google.com/chrome/answer/95647?hl=en
https://support.mozilla.org/en-US/kb/en ... references
http://www.opera.com/help/tutorials/security/cookies/
https://support.apple.com/en-us/HT201265
https://privacy.microsoft.com/en-us/win ... nd-privacy


As an example you can see this in the inspector. Right click on the language and you can delete just that cookie.
Once you are sure it is deleted you can refresh your browser and it should be in Dutch
Screen-Shot-2023-03-21-at-22.26.15.png

Screen-Shot-2023-03-21-at-22.26.15.png (82.29 KiB) Viewed 498 times


DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by Hourglasss » Wed Mar 22, 2023 7:54 am

by mona wrote:
Wed Mar 22, 2023 6:24 am
It should do that if the default is set to dutch, as an additional safeguard put sort order as dutch 1 and english 2
You can check by clearing your browser cookies or post your webpage and someone can check.

If you are not sure how to clear your cookies (and ideally you just want to your site cookies specifically) you can search google, these used to be correct.
https://support.google.com/chrome/answer/95647?hl=en
https://support.mozilla.org/en-US/kb/en ... references
http://www.opera.com/help/tutorials/security/cookies/
https://support.apple.com/en-us/HT201265
https://privacy.microsoft.com/en-us/win ... nd-privacy


As an example you can see this in the inspector. Right click on the language and you can delete just that cookie.
Once you are sure it is deleted you can refresh your browser and it should be in Dutch
Screen-Shot-2023-03-21-at-22.26.15.png
I tried clearing all site cookies and data and even using different browser it still auto detects the langauge of the browser and set the language to english if the browser is english

Active Member

Posts

Joined
Mon Jan 23, 2023 10:39 pm

Post by by mona » Wed Mar 22, 2023 8:21 am

Ok I misread that
? You browse in English but want it to default to Dutch ..

catalog/controller/startup/startup.php

replace

Code: Select all

			if (!$detect) { 
				// Try using language folder to detect the language
				foreach ($browser_languages as $browser_language) {
					if (array_key_exists(strtolower($browser_language), $languages)) {
						$detect = strtolower($browser_language);
						break;
					}
				}
			}
with (for example but you will have to check your language code - British English is en-gb)

Code: Select all

			if (!$detect) { 
				$detect= 'nl-nl';
			}

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am
Who is online

Users browsing this forum: No registered users and 101 guests