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

@colorfy-software/localify

v0.2.3

Published

Localization for React Native made simple

Downloads

55

Readme

🎯 Purpose

Localify helps you handle localization in your React Native apps with first class Typescript support: simply autocomplete your way through your translation files.

🏗️ Installation

yarn add @colorfy-software/localify react-native-localize i18n-js
npx pod-install ios --yes

💻 Usage

Setup

App

// ./index.tsx

import { AppRegistry } from 'react-native'
import Localify from '@colorfy-software/localify'

import App from './src/App'

const translations = {
  en: require('./src/locales/en.ts'),
  de: require('./src/locales/de.ts'),
} as const

Localify.init({
  // mandatory
  translations,
  // optional
  defaultSeparator: '.',
  // optional
  fallback: { languageTag: 'en', isRTL: false },
})

AppRegistry.registerComponent('main', () => App)

Jest

// ./jest.setup.js
// (or wherever you have your Jest config's setupFiles file)

process.env.JEST = true // add this line

With TypeScript & autocomplete

// ./src/locales/index.ts
import Localify, { ValueMapType, currentLocale, currentLocaleCode } from '@colorfy-software/localify'

// Example of what en.ts has to look like:

// export default {
//   general: {
//     activity: 'Activity',
//     home: 'Home',
//     settings: 'Settings',
//     tips: 'Tips',
//     logout: 'Log out',
//   },
//   errors: {
//     requiredField: 'This field is required',
//     passwordTooLong: 'Password needs to be less than **{{maxLengthValue}}** characters long.',
//     invalidEmail:
//       "Sorry, **{{email}}** is not a valid email address. Please double check the email you've entered and try again.",
//     passwordRules:
//       'Your password must be **at least 8 characters long**, with at least three of the following kinds of characters: **uppercase, lowercase, number, and/or symbols**.',
//   },
// } as const

// 👆 notice the `as const`.

import type en from './en'

type ContextType = keyof typeof en

const getLocalizedString = <
  C extends ContextType,
  K extends keyof typeof en[C],
  V extends ValueMapType<typeof en[C][K] extends string ? typeof en[C][K] : never>,
  R extends typeof en[C][K],
>(
  context: C,
  key: K,
  ...values: keyof V extends never ? [never?] : [V]
): R => Localify.getLocalizedString(context, key, values)

export { currentLocale, currentLocaleCode, getLocalizedString }

This is required so that you can define your preferred language for the TypeScript-powered autocompletion. Now, you'd have to import everything from ./src/locales/index.ts instead of the library, getLocalizedString() being the most important here:

// ./src/App.tsx
import * as React from 'react'
import { StyleSheet, SafeAreaView, Text } from 'react-native'

import { currentLocale, getLocalizedString } from './locales'

export default function App() {
  return (
    <SafeAreaView style={styles.container}>
      <Text>
        {currentLocale()}
        {getLocalizedString('general', 'home')}
        {getLocalizedString('errors', 'invalidEmail', { email: '[email protected]' })}
      </Text>
    </SafeAreaView>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 60,
    height: 60,
    marginVertical: 20,
  },
})

Without TypeScript/autocomplete

// ./src/App.tsx
import * as React from 'react'
import Localify from '@colorfy-software/localify'
import { StyleSheet, SafeAreaView, Text } from 'react-native'

export default function App() {
  return (
    <SafeAreaView style={styles.container}>
      <Text>
        {Localify.currentLocale()}
        {Localify.getLocalizedString('general', 'settings')}
        {Localify.getLocalizedString('errors', 'passwordTooLong', { maxLengthValue: '50' })}
      </Text>
    </SafeAreaView>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 60,
    height: 60,
    marginVertical: 20,
  },
})

🤝 Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

💖 Code of Conduct

This library has adopted a Code of Conduct that we expect project participants to adhere to. Please read the full text so that you can understand what actions will and will not be tolerated.

📰 License

localify is licensed under the MIT License.