npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

translate-components

v1.4.1

Published

🌎🌍🌏 translate-components provides a super simple translation for React.

Downloads

115

Readme

translate-components 🌎🌍🌏

Build Status

translate-components provides a super simple translation for React. Write your application in your language and use translate-components to annotate which texts should be translated.

Demo

preview

Features

  • Write in your language.
  • Instant translations without having to refresh your browser.
  • Easy to use.
  • Support for create-react-app.

Install

npm install --save translate-components

How to use

//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { TranslateProvider } from 'translate-components'
import './index.css';
import translations from './translations.json'

ReactDOM.render(
  <TranslateProvider translations={translations} defaultLanguage={'en'}>
    <App />
  </TranslateProvider>,
  document.getElementById('root')
);

Wrap your application with TranslateProvider, pass the translations and the default language of your application.

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Translate from 'translate-components'
import { reactTranslateChangeLanguage } from 'translate-components'

class App extends Component {
  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2><Translate>Welcome to React</Translate></h2>
        </div>
        <p className="App-intro">
          <Translate>To get started, edit</Translate> <code>src/App.js</code> <Translate>and save to reload.</Translate>
        </p>
        <div>
          <button onClick={reactTranslateChangeLanguage.bind(this, 'en')}>
            English
          </button>
          <button onClick={reactTranslateChangeLanguage.bind(this, 'es')}>
            Spanish
          </button>
          <button onClick={reactTranslateChangeLanguage.bind(this, 'de')}>
            German
          </button>
        </div>
      </div>
    );
  }
}

export default App;

Use Translate component to wrap the text you want to translate. Use reactTranslateChangeLanguage to change the language of your application. Pass to reactTranslateChangeLanguage function, as first argument, the language. This translate instantly your application!

Of course, you need to have a translation JSON file to pass it to TranslateProvider like this:

{
  "Welcome to React": {
    "es": "Bienvenido a React",
    "de": "Willkommen in React"
  },
  "To get started, edit": {
    "es": "Para empezar, edita",
    "de": "Um loszulegen, bearbeiten"
  },
  "and save to reload.": {
    "es": "y guarda para refrescar la pagina",
    "de": "und speichern, um neu zu laden"
  }
}

By default the Translate component wraps the tranlated text into a <span> element. Additionally, you can use the prop useRawText from the Translate component to render the tranlated text with no wrapping. This will be useful to render the text in elements that don't support nested <span> as well as for other user cases like placeholders in <input> elements. E.g.

<select className="selectClass">
  <option value="phone"><Translate useRawText={true}>Phone</Translate></option>
  <option value="email"><Translate useRawText={true}>Email</Translate></option>
  <option value="textMessage"><Translate useRawText={true}>Text Message</Translate></option>
</select>

<input type="text" name="phone" className="form-control phone" placeholder={<Translate useRawText={true}>NΓΊmero TelefΓ³nico</Translate>} />

The default language passed to TranslateProvider is the key of your translation objects.

Extracting strings

Once we have our app annotated with Translate component it's now time to extract these strings automatically. To extract these string we need to create a Javascript file that we will run with NodeJS. In this file we should import getTranslations:

let extractTranslations = require('./../translate-components/lib/getTranslations')
extractTranslations.extract('./src/*.js', './src/translations.json', ['es', 'de'])

extractTranslations.extract() needs three params:

  • Files RegExp
  • File translation output
  • Languages

If a translation file not exist, translate-components will create a file but if it already exist will add the new translations. Execute node pathToYourFile.js and that will generate a translations file!

Debug mode (Highlight texts)

Enable debug mode to higlight untranslated texts.

<TranslateProvider debugMode={true} translations={translations} defaultLanguage={'en'}>

When translate-components does not found a translate for a certain text this text will be higlighted with a background yellow. Demo:

highlight