-
Notifications
You must be signed in to change notification settings - Fork 3
add multilanguage pattern #3
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
fe977ec
f62338f
bd980e4
00f229e
a37b252
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { useTranslation } from 'react-i18next' | ||
| import { Button, Flex, Text } from 'theme-ui' | ||
|
|
||
| const LanguageSwitcher = () => { | ||
| const { i18n } = useTranslation() | ||
|
|
||
| const changeLanguage = (lng) => { | ||
| i18n.changeLanguage(lng) | ||
| } | ||
|
|
||
| const languages = [ | ||
| { code: 'en', name: 'English', flag: '🇺🇸' }, | ||
| { code: 'tr', name: 'Türkçe', flag: '🇹🇷' } | ||
| ] | ||
|
|
||
| return ( | ||
| <Flex sx={{ gap: 2, alignItems: 'center' }}> | ||
| <Text sx={{ fontSize: 1, fontWeight: 'bold' }}>Language:</Text> | ||
| {languages.map((lang) => ( | ||
| <Button | ||
| key={lang.code} | ||
| onClick={() => changeLanguage(lang.code)} | ||
| variant={i18n.language === lang.code ? 'primary' : 'outline'} | ||
| sx={{ | ||
| fontSize: 1, | ||
| py: 1, | ||
| px: 2, | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| gap: 1, | ||
| cursor: 'pointer', | ||
| opacity: i18n.language === lang.code ? 1 : 0.7, | ||
| '&:hover': { | ||
| opacity: 1 | ||
| } | ||
| }} | ||
| > | ||
| <span>{lang.flag}</span> | ||
| <span>{lang.name}</span> | ||
| </Button> | ||
| ))} | ||
| </Flex> | ||
| ) | ||
| } | ||
|
|
||
| export default LanguageSwitcher | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import Icon from './icon' | |
| import Flag from './flag' | ||
| import ScrollLock from 'react-scrolllock' | ||
| import NextLink from 'next/link' | ||
| import LanguageSwitcher from '../components/languageSwitcher/LanguageSwitcher' | ||
|
|
||
| const rgbaBgColor = (props, opacity) => | ||
| `rgba( | ||
|
|
@@ -147,6 +148,8 @@ const Navigation = props => ( | |
| <NextLink href="https://toolbox.hackclub.com/" passHref> | ||
| <Link>Toolbox</Link> | ||
| </NextLink> | ||
|
|
||
|
|
||
| </NavBar> | ||
| ) | ||
|
|
||
|
|
@@ -225,6 +228,7 @@ function Header({ unfixed, color, bgColor, dark, fixed, ...props }) { | |
| color={baseColor} | ||
| dark={dark} | ||
| /> | ||
| <LanguageSwitcher /> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainVerify responsive behavior of the language switcher. The LanguageSwitcher is well-positioned in the navigation. However, ensure it behaves appropriately on mobile devices where navigation collapses. 🏁 Script executed: #!/bin/bash
# Description: Check if LanguageSwitcher has responsive considerations
# Expected: Component should handle mobile layouts appropriately
# Look for responsive styles in the LanguageSwitcher component
rg -A 10 -B 5 "@media|sx.*display|mobile" components/languageSwitcher/LanguageSwitcher.jsLength of output: 91 We didn’t locate any responsive rules with the previous grep. Let’s first find the exact LanguageSwitcher file and then inspect it for breakpoints or media queries: 🏁 Script executed: #!/bin/bash
# 1. Locate the LanguageSwitcher component file
rg -l "export.*LanguageSwitcher" -g "*.js" -g "*.jsx" -g "*.ts" -g "*.tsx"
# 2. If found, inspect for responsive patterns
FILE=$(rg -l "export.*LanguageSwitcher" -g "*.js" -g "*.jsx" -g "*.ts" -g "*.tsx" | head -n1)
if [ -n "$FILE" ]; then
echo "🔍 Inspecting $FILE for responsive styling…"
echo "--- @media queries ---"
rg -n -C 3 "@media" "$FILE"
echo "--- sx props ---"
rg -n -C 3 "sx=" "$FILE"
echo "--- useMediaQuery hook ---"
rg -n -C 3 "useMediaQuery" "$FILE"
else
echo "❌ LanguageSwitcher component file not found"
fiLength of output: 1335 Ensure LanguageSwitcher adapts to mobile layouts The • File:
Suggestions:
🤖 Prompt for AI Agents |
||
| <ToggleContainer color={toggleColor} onClick={handleToggleMenu}> | ||
| <Icon glyph={toggled ? 'view-close' : 'menu'} /> | ||
| </ToggleContainer> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import i18n from 'i18next' | ||
| import { initReactI18next } from 'react-i18next' | ||
| import LanguageDetector from 'i18next-browser-languagedetector' | ||
|
|
||
| // Import translation files | ||
| import enTranslations from '../public/locales/en/translation.json' | ||
| import trTranslations from '../public/locales/tr/translation.json' | ||
|
|
||
| i18n | ||
| // Detect user language | ||
| .use(LanguageDetector) | ||
| // Pass the i18n instance to react-i18next | ||
| .use(initReactI18next) | ||
| // Initialize i18next | ||
| .init({ | ||
| debug: process.env.NODE_ENV === 'development', | ||
|
|
||
| fallbackLng: 'en', | ||
| interpolation: { | ||
| escapeValue: false, // not needed for react as it escapes by default | ||
| }, | ||
|
|
||
| // Language resources | ||
| resources: { | ||
| en: { | ||
| translation: enTranslations | ||
| }, | ||
| tr: { | ||
| translation: trTranslations | ||
| } | ||
| }, | ||
|
|
||
| // Language detection options | ||
| detection: { | ||
| // Order of language detection | ||
| order: ['querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag'], | ||
|
|
||
| // Keys or params to lookup language from | ||
| lookupQuerystring: 'lng', | ||
| lookupCookie: 'i18next', | ||
| lookupLocalStorage: 'i18nextLng', | ||
|
|
||
| // Cache user language | ||
| caches: ['localStorage', 'cookie'], | ||
|
|
||
| // Optional expire and domain for set cookie | ||
| cookieMinutes: 10080, // 7 days | ||
| }, | ||
|
|
||
| // React options | ||
| react: { | ||
| useSuspense: false | ||
| } | ||
| }) | ||
|
|
||
| export default i18n |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // Next.js i18n configuration for routing | ||
| const i18nConfig = { | ||
| locales: ['en', 'tr'], | ||
| defaultLocale: 'en', | ||
| localeDetection: true | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| export default i18nConfig | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Make the "Language:" label translatable.
The label is hardcoded in English, which defeats the purpose of internationalization. It should use a translation key.
And add the translation key to your locale files:
{ "language": "Language" }🤖 Prompt for AI Agents