Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@transifex/react

Package Overview
Dependencies
Maintainers
1
Versions
88
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@transifex/react

Transifex Native for React

  • 0.6.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.6K
decreased by-18.07%
Maintainers
1
Weekly downloads
 
Created
Source

Transifex Native for React

React component for localizing React application using Transifex Native.

Related packages:

Install

Install the library and its dependencies using:

npm install @transifex/native @transifex/react --save

Usage

T Component

import React from 'react';

import { T } from '@transifex/react';

function Example() {
  return (
    <div>
      <T _str="Hello world" />
      <T _str="Hello {username}" username={user} />
    </div>
  );
}

Available optional props:

PropTypeDescription
_contextStringString context, affects key generation
_keyStringCustom string key
_commentStringDeveloper comment
_charlimitNumberCharacter limit instruction for translators
_tagsStringComma separated list of tags

UT Component

import React from 'react';

import { UT } from '@transifex/react';

function Example () {
  return (
    <div>
      <UT _str="Hello <b>{username}</b>" username={user} />
      <p>
        <UT _str="Hello <b>{username}</b>" _inline username={user} />
      </p>
    </div>
  )
}

UT has the same behaviour as T, but renders source string as HTML inside a div tag.

Available optional props: All the options of T plus:

PropTypeDescription
_inlineBooleanWrap translation in span when _html is used

useT hook

Returns a state variable that will be automatically updated when the selected language changes. Used internally by the T and UT components. Accepts the same props as the T component.

You will most likely prefer to use the T or UT components over this, unless for some reason you want to have the translation output in a variable for manipulation.

import React from 'react';

import { useT } from '@transifex/react';

function Capitalized() {
  const message = useT('Hello world');
  return <span>{message.toUpperCase()}</span>;
}

useLanguages hook

Returns a state variable that will eventually hold the supported languages of the application. Makes an asynchronous call to the CDS.

import React from 'react';
import { useLanguages } from '@transifex/react';

function LanguageList () {
  const languages = useLanguages();
  return (
    <ul>
      {languages.map(({ code, name }) => (
        <li key={code}>
          <strong>{code}</strong>: {name}
        </li>
      ))}
    </ul>
  );
}

LanguagePicker component

Renders a <select> tag that displays supported languages and switches the application's selected language on change. Uses useLanguages internally.

import React from 'react';
import { T, LanguagePicker } from '@transifex/react';

function App () {
  return (
    <div>
      <T _str="This is a translatable message" />
      <LanguagePicker />
    </div>
  );
}

Accepts properties:

  • sourceLanguage: defaults to {code: 'en', name: 'English'}
  • className: The CSS class that will be applied to the <select> tag

If you want something different than a <select>, it should be easy to write your own language picker using useLanguages:

import React from 'react';
import { tx } from '@transifex/native';
import { useLanguages } from '@transifex/react';

function MyLanguagePicker () {
  const languages = useLanguages();

  return (
    <>
      <button onClick={() => tx.setCurrentLocale('en')}>
        English
      </button>
      {languages.map(({ code, name }) => (
        <button key={code} onClick={() => tx.setCurrentLocale(code)}>
          {name}
        </button>
      ))}
    </>
  );
}

License

Licensed under Apache License 2.0, see LICENSE file.

Keywords

FAQs

Package last updated on 21 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc