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

use-translations

v0.1.0

Published

Intl hook to provide better usage

Downloads

4

Readme

Welcome to use-translations 👋

React hook that format and dynamically types your translations.

alt Version alt License: MIT

use-translations use-translations

🏠 Homepage

Documentation

Usage

API

Install

npm i use-translations or yarn add use-translations

Usage

  • To use useTranslations hook, first you need to wrap component with <TranslationProvider> component. <TranslationProvider> takes 1 property that is formatMessage.
  • Because it is dynamic and supports multiple i18n libraries (react-intl, i18n and others), You have to provide formatting function as prop.
  • Provided translations needs to be as readonly array.

Simple hook example

const translationStrings = [
  'errors.toast',
  'errors.bookings.status',
  'errors.bookings.confirmation'
] as const;

const translations = useTranslations(translationStrings);

translations: {
  errorsToast: 'translation';
  errorsBookingsStatus: 'translation';
  errors.BookingsConfirmation: 'translation';
}

With react-intl example

https://github.com/Klosiek/use-translations/tree/main/example/ReactIntlExample.tsx

import { IntlContext, IntlProvider } from 'react-intl';
import { TranslationProvider, useTranslations } from 'use-translations';

const messages = {
  pl: {
    'form.post.code': 'Kod pocztowy react-intl',
  },
  en: {
    'form.post.code': 'Post code react-intl',
  },
};

export const ReactIntlExample = () => {
  const locale = 'en';

  return (
    <IntlProvider locale={locale} messages={messages[locale]}>
      <IntlContext.Consumer>
        {({ formatMessage }) => (
          <TranslationProvider formatMessage={id => formatMessage({ id })}>
            <MessageExample />
          </TranslationProvider>
        )}
      </IntlContext.Consumer>
    </IntlProvider>
  );
};

const MessageExample = () => {
  const testTranslations = ['form.post.code'] as const;
  const translations = useTranslations(testTranslations);

  return <>{translations.formPostCode}</>;
};

With i18n example

https://github.com/Klosiek/use-translations/tree/main/example/I18nExample.tsx

import i18n from 'i18next';
import { initReactI18next, useTranslation } from 'react-i18next';
import { TranslationProvider, useTranslations } from 'use-translations';

const messages = {
  pl: {
    'form.post.code': 'Kod pocztowy i18n',
  },
  en: {
    'form.post.code': 'Post code 18n',
  },
};

i18n.use(initReactI18next).init({
  resources: {
    pl: {
      translation: messages.pl,
    },
    en: {
      translation: messages.en,
    },
  },
});

i18n.changeLanguage('en');

export const I18nExample = () => {
  const { t } = useTranslation();

  return (
    <TranslationProvider formatMessage={id => t(id)}>
      <MessageExample />
    </TranslationProvider>
  );
};

const MessageExample = () => {
  const testTranslations = ['form.post.code'] as const;
  const translations = useTranslations(testTranslations);

  return <div>{translations.formPostCode}</div>;
};

For now it only supports translations separated with .

API

useTranslations

  • Props

| Prop name | Description | Default value | Example values | | --------- | ------------------------------------------------- | ------------- | --------------------------------------------- | | default | Array of translations to create dynamically types | [] | ['form.first.name', 'form.errors.first.name'] |

  • Returns

Hook return object of translations in camelCase provided in hook.

TranslationProvider

  • Props

| Prop name | Description | Default value | Example values | | ------------- | --------------------------------- | ------------- | --------------------------- | | formatMessage | Function provided by i18n library | null | (id) => formatMessage({id}) |

Authors

👤 Sebastian Kłosiński

Thanks to @Jaaneek for guidance.

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check the issues page.

Show your support

Give a ⭐️ if this project helped you!

PixelShiba