Giter Club home page Giter Club logo

Comments (3)

aralroca avatar aralroca commented on May 13, 2024 1

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.

from next-translate.

aralroca avatar aralroca commented on May 13, 2024 1

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'

from next-translate.

BjoernRave avatar BjoernRave commented on May 13, 2024

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

from next-translate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.