Git Product home page Git Product logo

react-translate's Introduction

React-Translate NPM Version NPM Downloads Actions Status Coverage Status

Easier i18n to be happier

react-translate can prevent you going crazy about react interpolation

Install

npm install @rogal/react-translate --save

Getting Started

import React from 'react'
import {
  TranslateProvider,
  useTranslate,
} from '@rogal/react-translate';

const Childen = () => {
  const {t} = useTranslate();
  return <p>{t('common:helloWorld')}</p>
}
const App = () => {
  const providerValue = {
    fallbackLng: 'en',
    languages: ['en', 'es'],
    translations: {
      en: {
        common: {
          helloWorld: 'Hello world'
        },
      },
      es: {
        common: {
          helloWorld: 'Hola Mundo'
        },
      }
    }
  };
  return (
    <TranslateProvider value={providerValue}>
      <Children />
    </TranslateProvider>
  )
}

How to instance a i18nConfiguration so you can use it outside of react

You only need to import the i18nConfiguration and passed the next config

import {i18nConfiguration} from '@rogal/react-translate';
import commonES from 'locales/es/common';
import commonEN from 'locales/en/common';

const i18next = i18nConfiguration({
  languages: ['es', 'en'],
    translations: {
      es: {
        common: commonES
      },
      en: {
        common: commonEN
      }
    },
    fallbackLng: 'en',
    language: 'en'
});

const translation = i18next.t('helloWorld');

More examples

You can check the demo folder or run the storybook so you can see advanced used cases

Components

  • A TranslateProvider to pass configuration data to the context.
  • useTranslate hook to use the t function in your code and translate jsonKeys to strings
  • withTranslate HOC to support class components
  • A Trans component to have a fallback when translating

Features

  • i18n languages supported such as English or Spanish.
  • Easy to use for dev experience
  • Works with SSR

How to contribute

You can install and have an enviroment ready for use with Storybook

  npm i
  npm start

License

MIT

react-translate's People

Contributors

gabrielseco avatar renovate-bot avatar renovate[bot] avatar

Watchers

 avatar  avatar

react-translate's Issues

Plurals

How plurals should work.

If you passed in the object options something bigger than 1 should see if we have the plural.

If we have it we got it and return it.

If not return the singular if we have it

Remove Context

We need to remove the exported context because it's a internal thing of the library

expose method switchLanguage, lang in hook

Fix interface context

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency serve to v14.2.3
  • chore(deps): update dependency husky to v9
  • chore(deps): update dependency lint-staged to v15
  • chore(deps): update dependency rollup to v4
  • chore(deps): update dependency typescript to v5
  • chore(deps): update storybook monorepo to v8 (major) (@storybook/addon-essentials, @storybook/react)
  • chore(deps): update testing-library monorepo (major) (@testing-library/jest-dom, @testing-library/react)
  • fix(deps): update dependency html-react-parser to v5
  • 🔐 Create all rate-limited PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/build.yml
  • actions/checkout v2
  • actions/setup-node v2
.github/workflows/ci.yml
  • actions/checkout v2
  • actions/setup-node v2
npm
demo/package.json
  • @rogal/react-translate 1.0.2
  • next 12.3.4
  • react 17.0.2
  • react-dom 17.0.2
package.json
  • html-react-parser 1.4.14
  • @rogal/front-linter 3.0.0-beta.10
  • @storybook/addon-essentials 6.5.9
  • @storybook/react 6.5.9
  • @testing-library/jest-dom 5.16.5
  • @testing-library/react 11.2.7
  • @types/jest 26.0.24
  • @types/node 14.18.63
  • babel-loader 8.3.0
  • coveralls 3.1.1
  • eslint-import-resolver-typescript 2.7.1
  • gh-pages 3.2.3
  • husky 4.3.8
  • identity-obj-proxy 3.0.0
  • jest 26.6.3
  • lint-staged 10.5.4
  • react 17.0.2
  • react-docgen-typescript-loader 3.7.2
  • react-dom 17.0.2
  • rollup 2.79.1
  • rollup-plugin-typescript2 0.36.0
  • serve 14.2.1
  • shipjs 0.26.3
  • ts-jest 26.5.6
  • typescript 4.9.5
  • react >= 16.12.0
  • react-dom >= 16.12.0

  • Check this box to trigger a request for Renovate to run again on this repository

Create plugin for storybook

Create a plugin for storybook so we can change i18n with a simple select knob.

At first we'll have it here as custom so we can develop then we release it as npm package

Pass property language to Provider

Set the provider with a language param for cases that people have the language in redux and the context is not observable after reloading page

Readme

Create a Readme

How to install

How to use

How to contribute

Changelog

Tags

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.