<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How can I get multi-language pages to switch based on browser language settings? in CMS Development</title>
    <link>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/807108#M34629</link>
    <description>&lt;P&gt;Hi Alyssa&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much, this is really helpful&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt; The only issue is that our default UK pages do not load with the language code in the URL. So for the UK, the page should load as&amp;nbsp;&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener"&gt;https://example.com/blog/examplepost&lt;/A&gt;&amp;nbsp;and the US version should load as&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener"&gt;https://example.com/en-us/blog/examplepost&lt;/A&gt;&amp;nbsp;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code currently redirects the US versions correctly, however when the browser language settings are set to UK English or anything else is the URL generates as&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener"&gt;https://example.com/en/blog/examplepost&lt;/A&gt;&amp;nbsp;when it should rather be&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener"&gt;https://example.com/blog/examplepost&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate your help further as every tweak I have tried has not been successful.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 14 Jun 2023 08:43:29 GMT</pubDate>
    <dc:creator>DigitalCloud</dc:creator>
    <dc:date>2023-06-14T08:43:29Z</dc:date>
    <item>
      <title>How can I get multi-language pages to switch based on browser language settings?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/806455#M34604</link>
      <description>&lt;P&gt;I have a website on HubSpot where I've created multi-language variants on some of the pages. The default pages are for the United Kingdom while the variants are for users in the United States.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I understand that HubSpot does not switch languages automatically based on IP or browser settings. I want the pages to switch to the US version of the page when the user's browser language settings are set to the US. Not every page, however, has a US variant.&lt;BR /&gt;&lt;BR /&gt;I tried to implement this javascript code to achieve that:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;lt;script type="text/javascript"&amp;gt;&lt;BR /&gt;var userLang = navigator.language || navigator.userLanguage;&lt;BR /&gt;var supportedLanguages = ['en-us', 'en']; // Replace with your supported languages&lt;BR /&gt;var defaultLanguage = 'en-us'; // Replace with your default language&lt;/P&gt;&lt;P&gt;// Check if the browser language is supported, otherwise redirect to the default language&lt;BR /&gt;if (supportedLanguages.indexOf(userLang) === -1) {&lt;BR /&gt;window.location.href = window.location.origin + '/' + defaultLanguage + window.location.pathname;&lt;BR /&gt;}&lt;BR /&gt;&amp;lt;/script&amp;gt;&lt;BR /&gt;&lt;BR /&gt;I tested it by changing my browser language settings to the US and then clicking on the default link. The link loads incorrectly as&amp;nbsp;&lt;A href="https://example/en-us/en-us/blog/examplepost" target="_blank" rel="noopener"&gt;https://example/en-us/en-us/blog/examplepost&lt;/A&gt;&amp;nbsp;instead of&amp;nbsp;&lt;A href="https://example/en-us/en-us/blog/examplepost" target="_blank" rel="noopener"&gt;https://example/en-us/blog/examplepost&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help or insight would be much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jun 2023 09:07:19 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/806455#M34604</guid>
      <dc:creator>DigitalCloud</dc:creator>
      <dc:date>2023-06-13T09:07:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get multi-language pages to switch based on browser language settings?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/806561#M34610</link>
      <description>&lt;P&gt;Sure, I can help you with that. Here are the steps on how to get multi&lt;A href="https://apkmircle.com/" target="_blank" rel="noopener"&gt;-&lt;/A&gt;language pages to switch based on browser language settings: Go to the Design tab in HubSpot. Click Edit on the page that you want to add language switching to. In the Code section, add the following JavaScript code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;var userLang = navigator.language || navigator.userLanguage;&lt;BR /&gt;var supportedLanguages = ['en-us', 'en']; // Replace with your supported languages&lt;BR /&gt;var defaultLanguage = 'en-us'; // Replace with your default language&lt;/P&gt;&lt;P&gt;// Check if the browser language is supported, otherwise redirect to the default language&lt;BR /&gt;if (supportedLanguages.indexOf(userLang) === -1) {&lt;BR /&gt;window.location.href = window.location.origin + '/' + defaultLanguage + window.location.pathname;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;Use code with caution. Learn more Save your changes. Test the page by changing your browser language settings to a supported language and then clicking on the link. The page should now load in the correct language. Here are some additional things to keep in mind: The JavaScript code above will only work for pages that have a US variant. If you have pages that do not have a US variant, you will need to add a separate JavaScript code snippet for each of those pages. The JavaScript code above will not work for pages that are accessed through a mobile device. If you want to make sure that the language is switched for mobile devices as well, you will need to use a different method, such as using the Accept-Language header. I hope this helps!&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jun 2023 12:31:52 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/806561#M34610</guid>
      <dc:creator>RDickman</dc:creator>
      <dc:date>2023-06-13T12:31:52Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get multi-language pages to switch based on browser language settings?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/806945#M34620</link>
      <description>&lt;P&gt;Hey,&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/581619"&gt;@RDickman&lt;/a&gt;&amp;nbsp;It looks like there are some inaccuracies in your reply.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The original code posted by&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/582929"&gt;@DigitalCloud&lt;/a&gt;,&amp;nbsp;already includes the JavaScript code you mentioned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on my level of understanding, the issue seems to be related to the appending of both the default language ('en-us') and the user's language to the URL, resulting in an incorrect link.&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/17186"&gt;@Anton&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/193060"&gt;@JBeatty&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/91608"&gt;@alyssamwilie&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/133120"&gt;@MatthiasWeber&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/146956"&gt;@BarryGrennan&lt;/a&gt;, do&amp;nbsp;you have any code examples you can share with&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/582929"&gt;@DigitalCloud&lt;/a&gt;? On how they can tackle this without the unwanted “doubling”?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regarding the point about mobile devices and the Accept-Language header, the JavaScript method using &lt;A href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language" target="_blank" rel="noopener"&gt;&lt;CODE&gt;navigator.language&lt;/CODE&gt;&lt;/A&gt; should work for mobile browsers as well as desktop browsers, since this is a standard property of the &lt;CODE&gt;navigator&lt;/CODE&gt; object in JavaScript.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lastly, the point about needing a separate JavaScript code snippet for each page that doesn't have a US variant might be misleading. Most scripts should be able to handle this case by checking if the user's language is supported, and falling back to the default language if it isn't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Again, thanks for your contribution, and I hope this clarifies some points! — Jaycee&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jun 2023 21:03:48 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/806945#M34620</guid>
      <dc:creator>Jaycee_Lewis</dc:creator>
      <dc:date>2023-06-13T21:03:48Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get multi-language pages to switch based on browser language settings?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/807037#M34626</link>
      <description>&lt;P&gt;The issue is that the code isn't checking if en-us is already in the path, so when it&amp;nbsp;&lt;EM&gt;is&amp;nbsp; &lt;/EM&gt;you end up with the language path in there twice. Here's how I would change the code to get it working properly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;&amp;lt;script type="text/javascript"&amp;gt;
  // Set a HubL variable to pull in languages of available translated pages
  {% set languages = content.translated_content.values()|selectattr('published')|map('language') %}
  // Append the current pages language to our languages HubL variable
  {% do languages.append(content.language) %}

  // add toLowerCase to userLang because some browsers use capitals and JavaScript is case sensitive
  const userLang = (navigator.language || navigator.userLanguage).toLowerCase();
  // Set our languages HubL variable as our supportLanguages
  const supportedLanguages = {{ languages|tojson }};
  // changed this to 'en' since you said not all pages have an 'en-us'
  const defaultLanguage = 'en';
  let path = window.location.pathname;
  // get first path (where langauge is usually set)
  let pathFirstString = path.split('/')[1];

  // check if first path matches one of our supported languages
  // if it does, update path variable to remove the first path
  if (supportedLanguages.indexOf(pathFirstString) !== -1) {
    path = '/' + path.split('/').slice(2).join('/');
  }

  // If userLang is not in supported languages
  // and our language path does not equal the defaultLanguage
  // go to the default language URL
  if (supportedLanguages.indexOf(userLang) === -1 &amp;amp;&amp;amp; pathFirstString != defaultLanguage) {
    window.location.href = window.location.origin + '/' + defaultLanguage + path;
  // else if our language path does not match the userLang
  // redirect to the userLang URL
  } else if (pathFirstString != userLang) {
     window.location.href = window.location.origin + '/' + userLang + path
  }
&amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 05:00:59 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/807037#M34626</guid>
      <dc:creator>alyssamwilie</dc:creator>
      <dc:date>2023-06-14T05:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get multi-language pages to switch based on browser language settings?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/807108#M34629</link>
      <description>&lt;P&gt;Hi Alyssa&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much, this is really helpful&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt; The only issue is that our default UK pages do not load with the language code in the URL. So for the UK, the page should load as&amp;nbsp;&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener"&gt;https://example.com/blog/examplepost&lt;/A&gt;&amp;nbsp;and the US version should load as&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener"&gt;https://example.com/en-us/blog/examplepost&lt;/A&gt;&amp;nbsp;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code currently redirects the US versions correctly, however when the browser language settings are set to UK English or anything else is the URL generates as&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener"&gt;https://example.com/en/blog/examplepost&lt;/A&gt;&amp;nbsp;when it should rather be&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener"&gt;https://example.com/blog/examplepost&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate your help further as every tweak I have tried has not been successful.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 08:43:29 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/807108#M34629</guid>
      <dc:creator>DigitalCloud</dc:creator>
      <dc:date>2023-06-14T08:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get multi-language pages to switch based on browser language settings?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/807207#M34631</link>
      <description>&lt;P&gt;No problem. What we can do here is add some more checks in our redirect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. If the user language is not one of the supported languages--check if the current path is equal to our split path and, if not, go to the default URL&lt;/P&gt;
&lt;P&gt;2. If the user language IS in the supported languages, first check if it's our default language. If it is, then check if the current path is equal to our split path and, if not, go to the default URL. If it's not our default language, go the languaged-path url.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;&amp;lt;script type="text/javascript"&amp;gt;
  // set a HubL variable to pull in languages of available translated pages
  {% set languages = content.translated_content.values()|selectattr('published')|map('language') %}
  // append the current pages language to our languages HubL variable
  {% do languages.append(content.language) %}

  // add toLowerCase to userLang because some browsers use capitals and JavaScript is case sensitive
  const userLang = (navigator.language || navigator.userLanguage).toLowerCase();
  // set our languages HubL variable as our supportedLanguages
  const supportedLanguages = {{ languages|tojson }};
  // set our default language
  const defaultLanguage = "en";
  // get current path
  let path = window.location.pathname;
  // get first path (where langauge is usually set)
  let pathFirstString = path.split('/')[1];

  // check if first path matches one of our supported languages
  // if it does, update path variable to remove the first path
  if (supportedLanguages.indexOf(pathFirstString) !== -1) {
    path = '/' + path.split('/').slice(2).join('/');
  }

  // If userLang is not in supported languages
  if (supportedLanguages.indexOf(userLang) === -1) {
    // and current path does not equal default path
    // go to default url
    if (path != window.location.pathname) {
      window.location.href = window.location.origin + path;
    }
  // else if our language path does not match the userLang
  } else if (pathFirstString != userLang) {
     // if userLang does not equal the defaultLanguage, go to userLang URL
     if (userLang != defaultLanguage) {
       window.location.href = window.location.origin + '/' + userLang + path
     // else if user is not currently on default page, go to default page
     } else if (path != window.location.pathname) {
       window.location.href = window.location.origin + path
     }
  }
&amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 12:23:15 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/807207#M34631</guid>
      <dc:creator>alyssamwilie</dc:creator>
      <dc:date>2023-06-14T12:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get multi-language pages to switch based on browser language settings?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/807679#M34645</link>
      <description>&lt;P&gt;Hi Alyssa&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do I need to add the above snippet to the previous one? When I use it and my browser language settings are set to English(UK) the URL now loads up as&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener nofollow noreferrer"&gt;https://example.com/en-gb/blog/examplepost&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;, When it should ideally load up as&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener nofollow noreferrer"&gt;https://example.com/blog/examplepost&lt;/A&gt;.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jun 2023 07:16:50 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/807679#M34645</guid>
      <dc:creator>DigitalCloud</dc:creator>
      <dc:date>2023-06-15T07:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get multi-language pages to switch based on browser language settings?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/807784#M34648</link>
      <description>&lt;P&gt;You should&amp;nbsp;&lt;EM&gt;replace&amp;nbsp;&lt;/EM&gt;the previous snippet with the new one.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does your page have one of it's languages set to English (UK)?&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jun 2023 11:25:05 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/807784#M34648</guid>
      <dc:creator>alyssamwilie</dc:creator>
      <dc:date>2023-06-15T11:25:05Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get multi-language pages to switch based on browser language settings?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/808949#M34679</link>
      <description>&lt;P&gt;Hi Alyssa,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The snippet works perfectly when my browser settings are set to;&amp;nbsp;&lt;SPAN&gt;English (United States), English, Afrikaans, German etc. The URL only changes when the browser language settings are set to English (United States), Which is exactly what I want.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;However, when the browser language settings are set to English (United Kingdom) the URL is generated with the ''en-gb'' language code added to the pathway. The page that I am testing it on is set to be an English-United Kingdom language variant in HubSpot. However, it is a primary page and the URL does not include the language code in it.&amp;nbsp; So it should generate as&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener nofollow noreferrer"&gt;https://example.com/blog/examplepost&lt;/A&gt;. And not&amp;nbsp;&lt;A href="https://example.com/blog/examplepost" target="_blank" rel="noopener nofollow noreferrer"&gt;https://example.com/en-gb/blog/examplepost.&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jun 2023 07:21:07 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/808949#M34679</guid>
      <dc:creator>DigitalCloud</dc:creator>
      <dc:date>2023-06-19T07:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get multi-language pages to switch based on browser language settings?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/809654#M34687</link>
      <description>&lt;P&gt;If English (United Kingdom) is your default language then in the code you need to change the defaultLanguage variable to "en-gb" instead of "en" as en-gb is the locale code for English (United Kingdom), not en.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2023 15:16:07 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-can-I-get-multi-language-pages-to-switch-based-on-browser/m-p/809654#M34687</guid>
      <dc:creator>alyssamwilie</dc:creator>
      <dc:date>2023-06-20T15:16:07Z</dc:date>
    </item>
  </channel>
</rss>

