LoginSignup
0
0

More than 3 years have passed since last update.

gatsby-theme-i18n-react-i18next: 初回アクセス時にブラウザの言語によって自動的に表示言語を変えたい

Posted at

https://github.com/gatsbyjs/themes/tree/master/starters/example-react-i18next
↑のスターターでアクセスしたときにブラウザの設定言語によって、表示言語を自動的に変わるようにしたい。

gatsby-browser.js
const i18nConfig = require("./i18n/config.json");

exports.onClientEntry = () => {
    const langs = i18nConfig.map(c => c.code);
    const userLang = navigator.language.substr(0, 2);
    if (langs.includes(userLang) && 
        !window.location.pathname.startsWith(`/${userLang}/`)
    ) {
      window.location.pathname = `/${userLang}${window.location.pathname}`;
    }
  }
i18n/config.json
[
  {
    "code": "en",
    "hrefLang": "en-US",
    "name": "English",
    "localName": "English",
    "langDir": "ltr",
    "dateFormat": "MM/DD/YYYY"
  },
  {
    "code": "de",
    "hrefLang": "de-DE",
    "name": "German",
    "localName": "Deutsch",
    "langDir": "ltr",
    "dateFormat": "DD.MM.YYYY"
  }
]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0