All Projects โ†’ pmmmwh โ†’ react-polyglot-hooks

pmmmwh / react-polyglot-hooks

Licence: MIT license
Hooks for using Polyglot.js with React.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to react-polyglot-hooks

redux-polyglot
Polyglot.js bindings for Redux
Stars: โœญ 59 (+126.92%)
Mutual labels:  i18n, polyglot
Polyglot
๐Ÿ”ค Multilingual and i18n support tool for Jekyll Blogs
Stars: โœญ 242 (+830.77%)
Mutual labels:  i18n, polyglot
fyodor
Convert your Amazon Kindle highlights and notes into markdown (or any format).
Stars: โœญ 101 (+288.46%)
Mutual labels:  i18n
laravel-react-boilerplate
Laravel React Boilerplate with Ant Design, Route-Level Code Splitting, Redux, Sanctum Auth
Stars: โœญ 49 (+88.46%)
Mutual labels:  react-hooks
react-ssr-starter
๐Ÿ”ฅ โš›๏ธ A React boilerplate for a universal web app with a highly scalable, offline-first foundation and our focus on performance and best practices.
Stars: โœญ 40 (+53.85%)
Mutual labels:  react-hooks
go-localize
i18n (Internationalization and localization) engine written in Go, used for translating locale strings.
Stars: โœญ 45 (+73.08%)
Mutual labels:  i18n
mini i18n
๐ŸŒ Minimalistic I18n library for Ruby
Stars: โœญ 93 (+257.69%)
Mutual labels:  i18n
D-i18n
ๅ‰็ซฏๅ›ฝ้™…ๅŒ–้€š็”จ่งฃๅ†ณๆ–นๆกˆใ€‚ๆŠนๅนณไธๅŒๅ‰็ซฏๅผ€ๅ‘ๆŠ€ๆœฏๆ ˆๆ‰€ๅธฆๆฅ็š„ๅทฎๅผ‚ใ€‚
Stars: โœญ 85 (+226.92%)
Mutual labels:  i18n
vue-example
Vue.js example application (server-side rendering, router, vuex store, form validation, i18n & l10n)
Stars: โœญ 62 (+138.46%)
Mutual labels:  i18n
jsbb
JavaScript building blocks
Stars: โœญ 31 (+19.23%)
Mutual labels:  react-hooks
react-use-comlink
Three ways to use Comlink web workers through React Hooks (and in a typesafe manner).
Stars: โœญ 39 (+50%)
Mutual labels:  react-hooks
markdown-i18n
i18n extension for Python Markdown
Stars: โœญ 13 (-50%)
Mutual labels:  i18n
elm-format-number
โœจFormat numbers as pretty strings
Stars: โœญ 56 (+115.38%)
Mutual labels:  i18n
forex-web-app
๐Ÿ’ฑโ€‚foreign currency exchange app built with react hooks
Stars: โœญ 17 (-34.62%)
Mutual labels:  react-hooks
react-without-redux
React State Management without Redux
Stars: โœญ 33 (+26.92%)
Mutual labels:  react-hooks
awesome-translations
๐Ÿ˜Ž Awesome lists about Internationalization & localization stuff. l10n, g11n, m17n, i18n. Translations! ๐ŸŒŽ๐ŸŒ
Stars: โœญ 54 (+107.69%)
Mutual labels:  i18n
advanced-spring-scaffold
This project provides an advanced baseline to help you kick start a Spring project.
Stars: โœญ 21 (-19.23%)
Mutual labels:  i18n
Domino-English-Translation
๐ŸŒ Let's translate Domino, a Japanese MIDI editor!
Stars: โœญ 29 (+11.54%)
Mutual labels:  i18n
pH7-Internationalization
๐ŸŽŒ pH7CMS Internationalization (I18N) package ๐Ÿ™Š Get new languages for your pH7CMS website!
Stars: โœญ 17 (-34.62%)
Mutual labels:  i18n
crowdin-api-client-ruby
The Crowdin Ruby Client is used to interact with the Crowdin API v2 from Ruby
Stars: โœญ 50 (+92.31%)
Mutual labels:  i18n

React Polyglot Hooks

Hooks for using Polyglot.js with React.

npm Package Minified Size Min-zipped Size

CircleCI Coverage Status Code Style Conventional Commits Dependabot Status Dependencies PeerDependencies DevDependencies

Installation

React Polyglot Hooks is available as an npm package.

// with npm
npm install react-polyglot-hooks

// with yarn
yarn add react-polyglot-hooks

React is required as a peer dependency. Please install version 16.8.3 or later (minimum requirement for hooks).

Usage

React Polyglot Hooks offers 1 wrapper component: <I18n>, 2 hooks: useLocale and useT and 1 helper component: <T>. The hooks and the helper component have to be wrapped with the <I18n> component to work properly.

Here is a quick example to get you started: First, wrap a parent component with <I18n> and provide locale and phrases.

Parent.jsx

import React from 'react';
import { I18n } from 'react-polyglot-hooks';
import Child from './Child';

// ... or any ways to determine current locale
const locale = window.locale || 'en';

// You can put translations in separate files
const phrases = {
  en: { hello: 'Hello, World!' },
  fr: { hello: 'Bonjour, Monde!' },
};

export default function Parent() {
  return (
    <I18n locale={locale} phrases={phrases[locale]}>
      <Child />
    </I18n>
  );
}

Then, in a child component, call the hooks:

Child.jsx

import React from 'react';
import { T, useLocale } from 'react-polyglot-hooks';

export default function Child() {
  const locale = useLocale(); // Current locale: "en"
  return (
    <React.Fragment>
      <span>{locale}</span>
      <T phrase="hello" />
    </React.Fragment>
  );
}

That's it! For more in-depth examples, check out the examples directory.

Usage with TypeScript

Types are baked in as the project is written in TypeScript.

API

<I18n>

Provides i18n context to the T component and the hooks. Accepts all options supported by Polyglot.js.

Props

Prop Type Required Description
children Node โœ… Any node(s) accepted by React.
locale string โœ… Current locale, used for pluralization.
phrases { [key: string]: string } โœ… Key-value pair of translated phrases, can be nested.
allowMissing boolean โŒ Controls whether missing phrase keys in a t call is allowed.
interpolation { prefix: string, suffix: string } โŒ Controls the prefix and suffix for an interpolation.
onMissingKey (key: string, options: InterpolationOptions, locale: string) => string โŒ A function called when allowMissing is true and a missing key is encountered.
pluralRules { pluralTypes: PluralTypes, pluralTypeToLanguages: PluralTypeToLanguages } โŒ Custom pluralization rules to be applied to change language(s) behaviour(s).

<T>

Renders a phrase according to the props.

Props

Props Type Required Description
phrase string โœ… Key of the needed phrase.
count number โŒ A number to be interpolated with smart_count.
fallback string โŒ A fallback to be rendered if the phrase is missing.
interpolations InterpolationOptions โŒ See InterpolationOptions below.

useLocale

Returns the current active locale (string).

useT

Returns a special function (t) which takes in parameters and returns a phrase.

t(phrase, InterpolationOptions)

Param Type Required Description
phrase string โœ… Key of the needed phrase.
InterpolationOptions number or { [key: string]: ReactNode } โŒ A number to be interpolated with smart_count, or a key-value pair to interpolate values into the phrase.

For more details, please visit the documentation of Polyglot.js.

Changelog

The changelog is available here.

License

This project is licensed under the terms of the MIT License.

Acknowledgements

This project is developed to ease the use of Polyglot.js within React, and is highly influenced by react-polyglot.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].