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

[FEATURE] Add i18n support #67

Open
guibranco opened this issue Jun 2, 2023 · 0 comments
Open

[FEATURE] Add i18n support #67

guibranco opened this issue Jun 2, 2023 · 0 comments
Labels
dependencies Pull requests that update a dependency file enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers help wanted Extra attention is needed i18n javascript Pull requests that update Javascript code

Comments

@guibranco
Copy link
Member

guibranco commented Jun 2, 2023

Description
To enhance the accessibility of our application, we need to implement internationalization (I18N) support. The application should support at least three languages: Portuguese (PT-BR), English (EN-US), and Spanish (ES-ES). This will allow users to interact with the application in their preferred language.

Acceptance Criteria

  • The application should be able to switch between the three specified languages.
  • Translations should be stored in separate files for better maintainability.
  • Ensure that all user-facing strings are translated.

Proposed Solution
We can use the react-i18next library for implementing I18N support. Below is a code example to demonstrate how to set up I18N support in the application.

Installation
First, install the required packages:

npm install i18next react-i18next

Setup Example
Create a i18n.js file for configuring I18N:

// src/i18n.js
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

i18n
  .use(initReactI18next)
  .init({
    resources: {
      en: {
        translation: {
          welcome: "Welcome to our application!",
        },
      },
      pt: {
        translation: {
          welcome: "Bem-vindo ao nosso aplicativo!",
        },
      },
      es: {
        translation: {
          welcome: "¡Bienvenido a nuestra aplicación!",
        },
      },
    },
    lng: "en", // default language
    fallbackLng: "en", // fallback language
    interpolation: {
      escapeValue: false, // React already escapes values
    },
  });

export default i18n;

Usage Example
Now, in your components, you can use the useTranslation hook to access the translations:

// src/App.js
import React from 'react';
import { useTranslation } from 'react-i18next';
import './i18n'; // import the i18n configuration

function App() {
  const { t } = useTranslation();

  return (
    <div>
      <h1>{t('welcome')}</h1>
      {/* Add language switcher here */}
    </div>
  );
}

export default App;

Language Switcher Example
You can implement a simple language switcher:

function LanguageSwitcher() {
  const { i18n } = useTranslation();

  const changeLanguage = (lng) => {
    i18n.changeLanguage(lng);
  };

  return (
    <div>
      <button onClick={() => changeLanguage('en')}>English</button>
      <button onClick={() => changeLanguage('pt')}>Português</button>
      <button onClick={() => changeLanguage('es')}>Español</button>
    </div>
  );
}

Additional Context
For more detailed guidance, refer to the Medium article on adding I18N to a React application.

@guibranco guibranco added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed dependencies Pull requests that update a dependency file javascript Pull requests that update Javascript code i18n labels Jun 4, 2023
@guibranco guibranco self-assigned this Jun 5, 2023
@guibranco guibranco removed their assignment Jan 26, 2024
@guibranco guibranco changed the title Add i18n support [FEATURE] Add i18n support May 15, 2024
@gitauto-ai gitauto-ai bot added the gitauto GitAuto label to trigger the app in a issue. label Jul 9, 2024
@ApiBR ApiBR deleted a comment from gitauto-ai bot Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers help wanted Extra attention is needed i18n javascript Pull requests that update Javascript code
Projects
None yet
Development

No branches or pull requests

1 participant