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

react-native-culture-text

v1.0.0

Published

A higher order component of <Text/> to provide i18n features in a react-friendly way

Downloads

16

Readme

React Native - Culture Text

A higher order component of <Text/> to provide i18n features in a react-friendly way

Motivation

Current i18n libraries implement translations in an app-wide manner. This generates a coupling that works againts component isolation. We need a way in which each component can define their owns translations, in a standalone manner. This is somehow similar to the concept handling local styles instead of having global rules.

Goals

  • dynamically set the content of <Text/> elements depending on the context's culture
  • message definition and theirs translations live inside each component
  • allow translations to be implemented with minimum friction of original code

Non-goals

  • provide a full-blown i18n library
  • be able to capture device's culture automatically (use your own mechanism for this)

Install

yarn add react-native-culture-text

Getting Started

  1. In your root component, wrap your whole UI with a <CultureContext locale={'es-AR'} /> to tell all the children in the hierarchy what's the current locale of the user.
  2. In each isolated component, wrap your UI with another <CultureContext translations={myTranslations} /> to tell the component's children where to look for translated messages.
  3. Switch your import statements to use the <Text/> component that comes with this library instead of the original react-native one.
  4. To each <Text/> element, add a new attribute messageKey that will be used to select which message translation to use.
  5. For those places that don't use <Text/> component, wrap your UI with a <CultureFragment/> component use the children render prop that provides a translate helper function

Example with component

import React, { Component } from 'react';
import {
  View
} from 'react-native';

import {
  CultureContext,
  Text
} from 'react-native-culture-text';

export default class example extends Component {
  render() {
    return (
      <CultureContext translations={translations} locale="es">
        <View>
          <Text>
            Welcome to React Native!
          </Text>
          <Text messageKey="welcome">
            This text depends on the selected culture
          </Text>
        </View>
      </CultureContext>
    );
  }
}

const translations = {
  en: {
    welcome: 'This text depends on the selected culture',
  },
  es: {
    welcome: 'Este texto depende de la cultura seleccionada',
  },
}

Example with render prop

import React, { Component } from 'react';
import {
  View,
  TextInput
} from 'react-native';

import {
  CultureContext,
  CultureFragment
} from 'react-native-culture-text';

export default class example extends Component {
  render() {
    return (
      <CultureContext translations={translations} locale="es">
        <CultureFragment>
          {T => (
            <View>
              <Text>
                Welcome to React Native!
              </Text>
              <TextInput placeholder={T('username')} />
            )}
          </View>
        </CultureFragment>
      </CultureContext>
    );
  }
}

const translations = {
  en: {
    username: 'type your username',
  },
  es: {
    username: 'ingrese su usuario',
  },
}