1.0.15 • Published 4 years ago

context-multi-language v1.0.15

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

context-multi-language

This library is for providing Multi Language System for React and React Native Applications.

Installation

yarn add context-multi-language
npm install context-multi-language

Usage

Add SKIP_PREFLIGHT_CHECK=true to an .env file in your project if you get eslint error.

TranslationsProvider

<TranslationsProvider
  remoteTranslationsUrl={remoteTranslationsUrl}
  defaultLocale="tr"
/>
PropTypeDefaultDescription
translationsobjectundefinedtranslations data
remoteTranslationsUrlstringundefinedfetch translations data from a remote endpoint
defaultLocalestringtrdefault language
errorComponentFCTranslationsErrordefault error component.
loadingComponentFCTranslationLoadingdefault loading component.
childrenFCTranslationsProvider's children.

useTranslations

const { t, changeLanguage, languages } = useTranslations();
PropTypeDescription
tobjectprovide translations data.
changeLanguagefunctionchange current language.
languagesarrayexisting languages.

Samples

import { TranslationsProvider, useTranslations } from 'context-multi-language';

const remoteTranslationsUrl = 'https://raw.githubusercontent.com/burhanyilmaz/translations/master/locales.json';

export default () => (
    <TranslationsProvider remoteTranslationsUrl={remoteTranslationsUrl} defaultLocale='tr'>
      <HomeScreen />
    </TranslationsProvider>
);

// or

const translations = {
  "languages": ["en"],
  "translations": {
    "en": {
      "password": "Password",
      "phoneNumber": "Phone Number",
      "signIn": "Sign in!"
    },
}

export default () => (
    <TranslationsProvider translations={translations} defaultLocale='tr'>
      <HomeScreen />
    </TranslationsProvider>
);
const HomeScreen = () => {
  const { t, changeLanguage, languages } = useTranslations();

  return (
    <SafeAreaView style={styles.fill}>
      <StatusBar barStyle="light-content" />
      <Input label={t.phoneNumber} />
      <Space />
      <Input label={t.password} secureTextEntry />
      <Space />
      <Button size="large">{t.singIn}</Button>
      <Space />
      <ButtonGroup>
        {languages.map((language) => (
          <Button
            key={language}
            size="tiny"
            onPress={() => changeLanguage(language)}
          >
            {language}
          </Button>
        ))}
      </ButtonGroup>
    </SafeAreaView>
  );
};

SS

React Native Mobile

React Web

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago