admin管理员组文章数量:1289565
I have set up some locales for our app which are uk
and us
. For the blog we can have either us/blog
or just /blog
for the uk
locale.
When I switch to us
like so: (locale = "us")
const handleRoute = (locale) => router.push(`${locale}${router.asPath}`)
the url gets correctly updated to have us/
prepended.
When I switch back to uk
using handleRoute
(locale= ""), it stays on us
, although router.asPath
is equal to /blog
Full ponent:
export const CountrySelector: React.FC = () => {
const router = useRouter()
const [selectedValue, setSelectedValue] = useState<string>(router.locale)
const handleOnChange = (countryValue) => {
setSelectedValue(countryValue)
}
const handleRoute = (locale) => router.push(`${locale}${router.asPath}`)
const selectedValueLoweredCase = selectedValue.toLowerCase()
const getCountryImageUrl = (countryLabel: string): string =>
`/images/flag-${countryLabel}-sm.png`
const getImageComponent = (countryLabel: string) => (
<Image
htmlWidth="25px"
src={getCountryImageUrl(countryLabel)}
alt={selectedValueLoweredCase}
/>
)
return (
<>
<div data-testid="country-selector">
{console.log(router)}
<Menu>
<MenuButton
as={Button}
variant="countrySelector"
rightIcon={<TriangleDownIcon marginTop="-6px" />}
>
<Flex align="baseline">
<Box
marginRight="12px"
display={selectedValueLoweredCase === "us" ? "none" : "block"}
>
{getImageComponent("uk")}
</Box>
<Box
marginRight="12px"
display={selectedValueLoweredCase === "uk" ? "none" : "block"}
>
{getImageComponent("us")}
</Box>
<Box color={colors.black["100"]}>{selectedValue}</Box>
</Flex>
</MenuButton>
<MenuList padding="0" borderRadius="0">
<MenuOptionGroup
onChange={(countryValue) => handleOnChange(countryValue)}
defaultValue={selectedValue}
type="radio"
>
<MenuItemOption value="UK" color="#000">
<Flex align="baseline">
<Box marginRight="10px">{getImageComponent("uk")}</Box>
<Box
onClick={() => handleRoute("")}
textTransform="uppercase"
color={colors.black["100"]}
>
united kingdom
</Box>
</Flex>
</MenuItemOption>
<MenuItemOption value="US">
<Flex align="baseline">
<Box marginRight="10px">{getImageComponent("us")}</Box>
<Box
onClick={() => handleRoute("us")}
textTransform="uppercase"
color={colors.black["100"]}
>
united states
</Box>
</Flex>
</MenuItemOption>
</MenuOptionGroup>
</MenuList>
</Menu>
</div>
</>
)
}
nextConfig.js:
...
i18n: {
localeDetection: false,
locales: getRegions(), // ["uk", "us"]
defaultLocale: getDefaultRegion(), // "uk"
},
...
I have set up some locales for our app which are uk
and us
. For the blog we can have either us/blog
or just /blog
for the uk
locale.
When I switch to us
like so: (locale = "us")
const handleRoute = (locale) => router.push(`${locale}${router.asPath}`)
the url gets correctly updated to have us/
prepended.
When I switch back to uk
using handleRoute
(locale= ""), it stays on us
, although router.asPath
is equal to /blog
Full ponent:
export const CountrySelector: React.FC = () => {
const router = useRouter()
const [selectedValue, setSelectedValue] = useState<string>(router.locale)
const handleOnChange = (countryValue) => {
setSelectedValue(countryValue)
}
const handleRoute = (locale) => router.push(`${locale}${router.asPath}`)
const selectedValueLoweredCase = selectedValue.toLowerCase()
const getCountryImageUrl = (countryLabel: string): string =>
`/images/flag-${countryLabel}-sm.png`
const getImageComponent = (countryLabel: string) => (
<Image
htmlWidth="25px"
src={getCountryImageUrl(countryLabel)}
alt={selectedValueLoweredCase}
/>
)
return (
<>
<div data-testid="country-selector">
{console.log(router)}
<Menu>
<MenuButton
as={Button}
variant="countrySelector"
rightIcon={<TriangleDownIcon marginTop="-6px" />}
>
<Flex align="baseline">
<Box
marginRight="12px"
display={selectedValueLoweredCase === "us" ? "none" : "block"}
>
{getImageComponent("uk")}
</Box>
<Box
marginRight="12px"
display={selectedValueLoweredCase === "uk" ? "none" : "block"}
>
{getImageComponent("us")}
</Box>
<Box color={colors.black["100"]}>{selectedValue}</Box>
</Flex>
</MenuButton>
<MenuList padding="0" borderRadius="0">
<MenuOptionGroup
onChange={(countryValue) => handleOnChange(countryValue)}
defaultValue={selectedValue}
type="radio"
>
<MenuItemOption value="UK" color="#000">
<Flex align="baseline">
<Box marginRight="10px">{getImageComponent("uk")}</Box>
<Box
onClick={() => handleRoute("")}
textTransform="uppercase"
color={colors.black["100"]}
>
united kingdom
</Box>
</Flex>
</MenuItemOption>
<MenuItemOption value="US">
<Flex align="baseline">
<Box marginRight="10px">{getImageComponent("us")}</Box>
<Box
onClick={() => handleRoute("us")}
textTransform="uppercase"
color={colors.black["100"]}
>
united states
</Box>
</Flex>
</MenuItemOption>
</MenuOptionGroup>
</MenuList>
</Menu>
</div>
</>
)
}
nextConfig.js:
...
i18n: {
localeDetection: false,
locales: getRegions(), // ["uk", "us"]
defaultLocale: getDefaultRegion(), // "uk"
},
...
Share
Improve this question
edited Oct 1, 2022 at 10:46
juliomalves
50.4k23 gold badges177 silver badges168 bronze badges
asked Jan 22, 2021 at 12:56
AessandroAessandro
5,77121 gold badges72 silver badges146 bronze badges
1 Answer
Reset to default 7When routing using localized routes, you need to specify the locale by including additional options in the router.push
call.
In your specific case, you can either do it by passing the desired locale in the options, and omitting it from the path:
const handleRoute = (locale) => router.push(router.asPath, router.asPath, { locale: locale })
Or specify it in the path directly but setting locale
to false
:
const handleRoute = (locale) => router.push(`${locale}${router.asPath}`, `${locale}${router.asPath}`, { locale: false })
Note that in both cases you'll need to pass the extra second param as
so that the options object can be passed too.
本文标签: javascriptNextjs router locale issueStack Overflow
版权声明:本文标题:javascript - Next.js router locale issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741401845a2376712.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论