Giter Site home page Giter Site logo

lofwalter / react-i18n Goto Github PK

View Code? Open in Web Editor NEW

This project forked from strikingly/react-i18n

0.0 0.0 0.0 137 KB

Gettext style i18n in React that supports component interpolation.

License: BSD 3-Clause "New" or "Revised" License

JavaScript 100.00%

react-i18n's Introduction

react-i18n

React i18n

npm i r-i18n --save

Usage

import React from 'react'

import {
  createI18n
  debug, 
} from 'r-i18n'

Samples

init

Use Jed to initialize i18n in your project.

const i18n = createI18n({ /* jed options */ })) 

t

Component as placeholder

const {t} = i18n

t('Welcome to Strikingly')  
// -> '欢迎使用 Strikingly'

t('Welcome to Strikingly, %{username}', { username: 'Shu' }) 
// -> '欢迎使用 Strikingly,Shu'

// React component as placeholder

t('%{author} assigned this event to %{assignee}', {
  author: <Author value={author} />,
  assignee: <em>[email protected]</em>
})
// -> [<Author value={author} />, ' assigned this event to ', <em>[email protected]</em>]

tct

HTML inside translated string with a root wrapper

const {tct} = i18n

//...

tct('Welcome. Click [link:here]', {
  root: <p/>,
  link: <a href="#" />
})
// -> <p>欢迎。点击 <a href="#">此处</a> 继续。</p>

Debug & mark

Wrap t and tct with a wrapper <span class="translation-wrapper"/>

(for React Native, it just appends a flag emoji to the message)

import {createI18n, debug} from 'r-i18n'

//...
const i18n = createI18n({ /* jed options */ })) 
debug()

i18n.tct('Welcome. Click [link:here]', {
  root: <p/>,
  link: <a href="#" />
})
// -> <span class="translation-wrapper">
//      <p>欢迎。点击 <a href="#">此处</a> 继续。</p>
//    </span>

React Native

React-i18n works both with React and React Native.
For React Native, use npm i rn-i18n --save.

React Native branch: react-native

Syntax

r-i18n uses %{} to pass args.
And use %% to escape %.

i.e. t('%{percentage}%% correct', { percentage: 100 }) -> '100% correct'

Acknowledgement

Post about i18n and React on Sentry blog: link.

Sentry source code on GitHub.

License of Sentry.

License of sprintf.js

react-i18n's People

Contributors

shuding avatar dfguo avatar rachelin avatar

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.