QuiiBz / next-international

Type-safe internationalization (i18n) for Next.js
https://next-international.vercel.app
MIT License
1.26k stars 59 forks source link

UseChangeLocale and set the urlMappingStrategy: 'rewrite' can't work together #136

Closed zhengjunxiang closed 1 year ago

zhengjunxiang commented 1 year ago

I use useChangeLocale to change locale when set the urlMappingStrategy: 'rewrite', and the url is start with locale

// middleware.ts
import { createI18nMiddleware } from 'next-international/middleware';
import { NextRequest } from 'next/server';

const I18nMiddleware = createI18nMiddleware(['en', 'zh'] as const, 'zh', {
  urlMappingStrategy: 'rewrite'
});
export function middleware(request: NextRequest) {
  return I18nMiddleware(request);
}

export const config = {
  matcher: ['/((?!api|image|_next/static|_next/image|favicon.ico).*)']
};
import { useCurrentLocale, useI18n, useChangeLocale } from '@/locales/client';

export default function Switch() {
const locale = useCurrentLocale();
  const changeLocale = useChangeLocale()

  const handleChangeLanguage = () => {
    changeLocale(locale === 'zh' ? 'en' : 'zh')
  }

  const items: MenuProps['items'] = [
    {
      key: '1',
      label: (
        <div onClick={handleChangeLanguage}>
          {
            locale === 'en' ? t('zh') : t('en')
          }
        </div>
      ),
    }
  ];

  return (
    <div className="text-base leading-8">
      <Dropdown menu={{ items }}>
        <div className='cursor-pointer flex items-center'>
          { locale === 'zh' ? t('zh')  : t('en') }&nbsp;&nbsp;&nbsp;
          <DownOutlined />
        </div>
      </Dropdown>
    </div>
  );
}

I hope the url doesn't include a locale, thinks

QuiiBz commented 1 year ago

Thanks, I've released 0.9.3 which includes #137 and that should fix your issue. Feel free to open another issue if it's not the case!