0.0.8 • Published 4 years ago

@mihanizm56/i18n-react v0.0.8

Weekly downloads
36
License
MIT
Repository
github
Last release
4 years ago

@mihanizm56/i18n-react

Sollution for i18n for react&redux usage based on intl-messageformat library (format.js)

Examples of usage

installation

npm install @mihanizm56/i18n-react

connection to redux

import { applyMiddleware, createStore } from "redux";
import { rootReducer } from "./root-reducer";
import { translationMiddleware, translationStorage } from "@mihanizm56/i18n-react";
import { fetchLanguageKeysRequest } from "../../services/api/requests/fetch-language-keys";

const rootReducer = combineReducers({
  translationStorage,
    // ...other reducers
});

export const createAppStore = () => {
  const store = createStore(
    rootReducer,
    applyMiddleware(
      translationMiddleware(fetchLanguageKeysRequest)
    )
  );

  return store;
};

action to make request to the backend to get i18n dictionary

import { fetchLangAction } from "@mihanizm56/i18n-react";

Component-based api

import React, { memo } from "react";
import { TranslationComponent } from "@mihanizm56/i18n-react";

export const CardWithComponent = memo(() => (
  <p>
    <TranslationComponent key="card-text" options={{foo:'bar'}}/>
  </p>
));

Render-props-based api

import React, { memo } from "react";
import { TranslationRenderProps } from "@mihanizm56/i18n-react";

export const CardRenderProps = memo(({ i18n }) => (
  <TranslationRenderProps>
    {({ i18n }) => <p>{i18n("card-text", {foo:'bar'})}</p>}
  </TranslationRenderProps>
));

HOC-based api

import React, { memo } from "react";
import { TranslationHOC } from "@mihanizm56/i18n-react";

export const TranslatedCard = memo(({ i18n }) => (
  <div>{i18n("card-text", {foo:'bar'})}</div>
));

export const CardHOC = TranslationHOC(TranslatedCard);

example of the request to get i18n dictionary

export const fetchLanguageKeysRequest = (lang) =>
  fetch(`http://google.com?lang=${lang}`).then((data) => data.json());

!!! Attention !!!

Response of the request must have that fields:

  • error (boolean) - the flag of the response status
  • errorText (string) - the main error message from the backend
  • data (object) - the main data from the backend
0.0.8-beta.10

4 years ago

0.0.8

4 years ago

0.0.8-beta.2

4 years ago

0.0.8-beta.1

4 years ago

0.0.8-beta.0

4 years ago

0.0.5-beta.16

4 years ago

0.0.5-beta.15

4 years ago

0.0.5-beta.14

4 years ago

0.0.6

4 years ago

0.0.5-beta.12

4 years ago

0.0.5-beta.13

4 years ago

0.0.5-beta.11

4 years ago

0.0.5-beta.10

4 years ago

0.0.5-beta.9

4 years ago

0.0.5-beta.8

4 years ago

0.0.5-beta.7

4 years ago

0.0.5-beta.6

4 years ago

0.0.5-beta.5

4 years ago

0.0.5-beta.3

4 years ago

0.0.5-beta.4

4 years ago

0.0.5-beta.2

4 years ago

0.0.5-beta.1

4 years ago

0.0.5-beta.0

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.2-beta.6

4 years ago

0.0.2-beta.4

4 years ago

0.0.2-beta.5

4 years ago

0.0.1

4 years ago

0.0.2-beta.3

4 years ago

0.0.2-beta.0

4 years ago

0.0.2-beta.1

4 years ago

0.0.2-beta.2

4 years ago

0.0.1-beta.0

4 years ago