Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add documentation on how to change language #43

Closed
BjoernRave opened this issue Feb 7, 2020 · 3 comments · Fixed by #47
Closed

add documentation on how to change language #43

BjoernRave opened this issue Feb 7, 2020 · 3 comments · Fixed by #47
Assignees
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@BjoernRave
Copy link
Contributor

It would be good if you could explain somewhere what is the best way to switch between languages, maybe even expose a changeLang function in the useTranslation hook

@aralroca
Copy link
Owner

aralroca commented Feb 7, 2020

Ok! For the simple way as possible, you don't need any API of this lib to change the language. You can do it directly with <Link> component or Router.push of next.js.

import React from 'react'
import useTranslation from 'next-translate/useTranslation'

import i18nConfig from '../i18n.json'

function ChangeLanguage() {
  const { t, lang } = useTranslation()

  return i18nConfig.allLanguages.map(lng => {
    if(lng === lang) return null

    const languageName = t(`layout:lang-${lng}`)
     // Or you can attach the current pathame at the end
    const href = `/${lang}`

    return (
      <Link href={href} key={lng}>
        {languageName}
      </Link>
    )
  })
}

We can provide some examples in the docs. But there isn't a changeLang function.

@aralroca aralroca added the documentation Improvements or additions to documentation label Feb 11, 2020
@aralroca aralroca self-assigned this Feb 11, 2020
@aralroca aralroca added this to the 0.7.0 milestone Feb 11, 2020
@BjoernRave
Copy link
Contributor Author

BjoernRave commented Feb 12, 2020

I came up with this function to change the language, I might do a PR to the lib to add this function to useTranslation once I figure out how to access router

  const changeLanguage = (lng: string) => {
    if (router.pathname === "/") {
      router.push(`/${lng}`);
    } else {
      let routes = router.pathname.split("/");
      if (allLanguages.includes(routes[1])) {
        routes[1] = lng;
      } else {
        routes.splice(1, 0, lng);
      }
      router.push(routes.join("/"));
    }
  };

It would also be good to have a Link component which extends the Link Component from next.js, but persists the language on route changes

This is my custom Link Component:

import NextLink, { LinkProps } from 'next/link'
import { useRouter } from 'next/router'
import { FC } from 'react'
import { allLanguages, defaultLanguage } from '../i18n.json'

const Link: FC<LinkProps> = ({ children, href, as, ...props }) => {
  const router = useRouter()

  const getCurrentLanguage = () => {
    if (router.pathname === '/') {
      return defaultLanguage
    } else {
      let routes = router.pathname.split('/')
      if (allLanguages.includes(routes[1])) {
        return routes[1]
      } else {
        return defaultLanguage
      }
    }
  }

  return (
    <NextLink
      href={`/${getCurrentLanguage()}${href === '/' ? '' : href}`}
      as={`/${getCurrentLanguage()}${as}`}
      {...props}>
      {children}
    </NextLink>
  )
}

export default Link

@aralroca
Copy link
Owner

Ok @BjoernRave, if you do a PR of Link and changeLanguage we can add them to our API. However, I am in favor of do it separately of useTranslation hook, in order to take advantage of tree-shaking. Because in most cases you need to change the language in a modal, and then in all other pages you don't need that extra code. And we want to keep a tiny size of this lib.

Ex of possible imports:

import Link from 'next-translate/Link'
import changeLanguage from 'next-translate/changeLanguage'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants