-
Notifications
You must be signed in to change notification settings - Fork 377
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
Simple, minimal i18n Solution #614
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…them extensible via a strict type inside `types.ts` file, and created a helper function to return the correct string for a given key and language. # Aider chat conversation: USER: create a simple locale strings using js objects and make them extensible via a strict type inside types.ts file, create a helper function to return the correct string for a given key, lang. ASSISTANT: To create a simple locale strings using JavaScript objects and make them extensible via a strict type inside `types.ts` file, and create a helper function to return the correct string for a given key, lang, we need to modify `copilot-widget\lib\locales\en.locale.ts`, `copilot-widget\lib\locales\ar.locale.ts`, and `copilot-widget\lib\locales\types.ts`: 1. Define the Locale type in `types.ts`. 2. Create the English locale object in `en.locale.ts`. 3. Create the Arabic locale object in `ar.locale.ts`. 4. Create a helper function to return the correct string for a given key and language. Here are the *SEARCH/REPLACE* blocks: copilot-widget\lib\locales\types.ts ```typescript <<<<<<< SEARCH ======= export type Locale = { [key: string]: string; }; >>>>>>> REPLACE ``` copilot-widget\lib\locales\en.locale.ts ```typescript <<<<<<< SEARCH ======= import { Locale } from './types'; const enLocale: Locale = { greeting: 'Hello', farewell: 'Goodbye', // Add more strings as needed }; export default enLocale; >>>>>>> REPLACE ``` copilot-widget\lib\locales\ar.locale.ts ```typescript <<<<<<< SEARCH ======= import { Locale } from './types'; const arLocale: Locale = { greeting: 'مرحبا', farewell: 'وداعا', // Add more strings as needed }; export default arLocale; >>>>>>> REPLACE ``` copilot-widget\lib\locales\helper.ts ```typescript <<<<<<< SEARCH ======= import enLocale from './en.locale'; import arLocale from './ar.locale'; function getLocaleString(key: string, lang: 'en' | 'ar'): string { const locale = lang === 'en' ? enLocale : arLocale; return locale[key] || ''; } export default getLocaleString; >>>>>>> REPLACE ```
…ap all imported languages as key-value pairs. # Aider chat conversation: USER: modify the function implementation to add more languages, you should try to map all imported languages as a key-value pairs and let the function choose conditionally ASSISTANT: To modify the function implementation to add more languages, we need to modify `copilot-widget\lib\locales\helper.ts`: 1. Import all the locale files. 2. Map all imported languages as key-value pairs. 3. Modify the getLocaleString function to choose the correct language conditionally. Here is the *SEARCH/REPLACE* block: copilot-widget\lib\locales\helper.ts ```typescript <<<<<<< SEARCH import enLocale from './en.locale'; import arLocale from './ar.locale'; function getLocaleString(key: string, lang: 'en' | 'ar'): string { const locale = lang === 'en' ? enLocale : arLocale; return locale[key] || ''; } export default getLocaleString; ======= import enLocale from './en.locale'; import arLocale from './ar.locale'; // Import more locales as needed const locales = { en: enLocale, ar: arLocale, // Add more locales as needed }; function getLocaleString(key: string, lang: keyof typeof locales): string { const locale = locales[lang]; return locale ? locale[key] || '' : ''; } export default getLocaleString; >>>>>>> REPLACE ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.