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

Simple, minimal i18n Solution #614

Merged
merged 9 commits into from
Feb 7, 2024
Merged

Simple, minimal i18n Solution #614

merged 9 commits into from
Feb 7, 2024

Conversation

ah7255703
Copy link
Collaborator

No description provided.

…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
```
@ah7255703 ah7255703 merged commit 3396400 into main Feb 7, 2024
3 checks passed
@ah7255703 ah7255703 deleted the widget/i18n-strings branch February 7, 2024 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant