Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

added alternate language URLs & changed language detection mechanism

Blue Mouseef7af13039df
diff --git a/public/index.html b/public/index.html
index 563c3c523..48e71f1a8 100644
--- a/public/index.html
+++ b/public/index.html
@@ -17,6 +17,7 @@
<meta property="og:image" content="https://swetrix.com/assets/og_image.png" />
<meta property="og:site_name" content="Swetrix Analytics" />
<meta property="og:url" content="https://swetrix.com">
+ <meta name="google" content="notranslate" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link href="/index.css" rel="stylesheet" />
diff --git a/src/components/Title.jsx b/src/components/Title.jsx
index 526e0a4b4..33567a144 100644
--- a/src/components/Title.jsx
+++ b/src/components/Title.jsx
@@ -1,9 +1,10 @@
import React from 'react'
import { Helmet } from 'react-helmet-async'
import _isEmpty from 'lodash/isEmpty'
+import _map from 'lodash/map'
import PropTypes from 'prop-types'
-import { TITLE_SUFFIX } from 'redux/constants'
+import { TITLE_SUFFIX, whitelist } from 'redux/constants'
const Title = ({ title, children }) => {
if (_isEmpty(title)) {
@@ -18,11 +19,16 @@ const Title = ({ title, children }) => {
}
const displayTitle = `${title} ${TITLE_SUFFIX}`
+ const pageURL = `${window.location.origin}${window.location.pathname}`
return (
<>
<Helmet>
<title>{displayTitle}</title>
+ {_map(whitelist, (lang) => (
+ <link key={lang} rel='alternate' hreflang={lang} href={`${pageURL}?lng=${lang}`} />
+ ))}
+ <link rel='alternate' hreflang='x-default' href={pageURL} />
</Helmet>
{children}
</>
diff --git a/src/i18next.js b/src/i18next.js
index f16d7625d..3cc83f5a7 100644
--- a/src/i18next.js
+++ b/src/i18next.js
@@ -4,12 +4,23 @@ import backend from 'i18next-http-backend'
import _isString from 'lodash/isString'
import _includes from 'lodash/includes'
import { whitelist, defaultLanguage } from 'redux/constants'
-import { setItem } from 'utils/localstorage'
+import { setItem, getItem } from 'utils/localstorage'
const lngDetector = new LanguageDetector()
lngDetector.addDetector({
name: 'customDetector',
lookup() {
+ if (window.localStorage) {
+ const lsLang = getItem('language')
+
+ if (_includes(whitelist, lsLang)) {
+ return lsLang
+ } else {
+ setItem('language', defaultLanguage)
+ setItem('i18nextLng', defaultLanguage)
+ }
+ }
+
const language = navigator.language || navigator.userLanguage
if (_isString(language)) {
@@ -22,9 +33,6 @@ lngDetector.addDetector({
return defaultLanguage
},
- cacheUserLanguage(lng) {
- setItem('language', lng)
- },
})
i18next
@@ -35,8 +43,7 @@ i18next
loadPath: '/locales/{{lng}}.json',
},
detection: {
- order: ['localStorage', 'customDetector'],
- lookupLocalStorage: 'language',
+ order: ['querystring', 'customDetector'],
},
interpolation: {
escapeValue: false,
@@ -53,4 +60,9 @@ i18next
defaultNS: 'common',
})
+i18next.on('languageChanged', (lng) => {
+ document.documentElement.setAttribute('lang', lng)
+ setItem('language', lng)
+})
+
export default i18next
diff --git a/src/utils/localstorage.js b/src/utils/localstorage.js
index 49838c53e..c76b6f4e3 100644
--- a/src/utils/localstorage.js
+++ b/src/utils/localstorage.js
@@ -8,7 +8,7 @@ const currentLocalStorage = {}
export const setItem = (key, value) => {
currentLocalStorage[key] = value
- localStorage.setItem(key, JSON.stringify(value))
+ localStorage.setItem(key, value)
}
export const getItem = (key) => {