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

Fix/components localization #139

Closed
wants to merge 13 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/components/locale-switcher/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import styles from './styles'
import { Disclosure, DisclosureContent, useDisclosureState } from 'reakit'
import { LocaleOption } from '@vtex/brand-ui/dist/components/Header/LocaleSwitcher'
import navigationData from '../../../public/navigation.json'
import { LibraryContext } from '@vtexdocs/components'
import { useContext } from 'react'

interface OptionProps {
screen: 'mobile' | 'large'
Expand Down Expand Up @@ -108,25 +110,28 @@ export default function LocaleSwitcher() {
},
]

// Getting locale and setlocale function from the context
const { locale: currentLocale, setLocale } = useContext(LibraryContext)
type LocaleType = typeof currentLocale

const handleOptionClick = async (option: string) => {
const locale = option as keyof Slug
const chosenLocale = option as LocaleType
const currentPath = window.location.pathname
const pathParts = currentPath.split('/')

// Obtain the current locale
const allowedLocales = ['en', 'es', 'pt']
const currentLocale = allowedLocales.includes(pathParts[1])
? pathParts[1]
: 'en'
const newPathParts = pathParts.filter((part) => part !== currentLocale)

// Set locale in context
setLocale(chosenLocale)

if (currentPath.includes('/docs')) {
const contentType = newPathParts[2]
const currentSlug = newPathParts[3]
const localizedSlug = await findLocalizedSlug(currentSlug, locale)
const localizedSlug = await findLocalizedSlug(currentSlug, chosenLocale)
const newPath = currentSlug
? `/${locale}/docs/${contentType}/${localizedSlug}`
: `/${locale}/docs/${contentType}`
? `/${chosenLocale}/docs/${contentType}/${localizedSlug}`
: `/${chosenLocale}/docs/${contentType}`
console.log(newPath)
window.location.href = newPath
} else if (
Expand All @@ -136,15 +141,15 @@ export default function LocaleSwitcher() {
) {
const contentType = newPathParts[1]
const currentSlug = newPathParts[2]
const localizedSlug = await findLocalizedSlug(currentSlug, locale)
const localizedSlug = await findLocalizedSlug(currentSlug, chosenLocale)
const newPath = currentSlug
? `/${locale}/${contentType}/${localizedSlug}`
: `/${locale}/${contentType}`
? `/${chosenLocale}/${contentType}/${localizedSlug}`
: `/${chosenLocale}/${contentType}`
console.log(newPath)
window.location.href = newPath
} else {
console.log(currentPath)
const newPath = `/${locale}`
const newPath = `/${chosenLocale}`
window.location.href = newPath
}
}
Expand Down