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

laravel-like-translations

v1.2.0

Published

Laravel-inspired translation/internationalization library

Downloads

21

Readme

laravel-like-translations

Javascript internalisation helper with nested translations and dot notation access.

Features

  • Pluralisation
  • Placeholder replacements with automatic capitalisation and turn to uppercase
  • Typescript-based autocomplete

Heavily inspired by Laravel's internationalisation system

Installation

npm: npm i laravel-like-translations

yarn: yarn add laravel-like-translations

Usage

1. Prepare your translation objects.

export interface TranslationShape {
    key1: string,
    keys: {
        a: string,
        b: string,
    },
}
import { TranslationShape } from "./types";

const En: TranslationShape = {
    key1: "Key 1",
    keys: {
        a: "A",
        b: "B",
    },
};

export default En;

2. Make your translation helper.

import makeTranslator from "laravel-like-translations";
import { TranslationShape } from "./types";
import En from "./lang/en";

const translator = makeTranslator<TranslationShape>({
    translations: {
        en: En,
    },
    // Optional fallback locale
    fallbackLocale: 'en',
});

export default translator;

3. Use your translation helper

import translator from "./helpers/translator";
// ...
const translated = translator("key1", "en");

Pluralisation and placeholders

Third argument allows you to engage pluralisation and placeholder replacement. Number is used in :count placeholder:

"I have one apple|I have :count apples"
translate(..., ..., 4)    // "I have 4 apples"
translate(..., ..., 1)    // "I have one apple"

String is used in :default placeholder:

"Your username is :default"
translate(..., ..., "cool_nickname")    // "Your username is cool_nickname"

You can also pass an object, where keys correspond to names of placeholders:

translate(..., ..., {
    name: "john"
});
"My name is :name" => "My name is john"
"My name is :Name" => "My name is John"
"My name is :NAME" => "My name is JOHN"

React implementation

Create custom hook that retrieves locale and subscribes to locale changes

import makeTranslator from "laravel-like-translations";
import { Paths, TranslationReplacements } from "laravel-like-translations/lib/types";
import translator from "./helpers/translator";
import { TranslationShape } from "./types";

function useTranslate() {
    // Implement your locale extractor, for example:
    const locale = useSelector(state => state.app.locale);

    // Create HOC for your translation helper
    function translate(key: Paths<TranslationShape>, replacements?: TranslationReplacements) {
        // HOC inserts extracted locale for you
        return translator(key, locale, replacements);
    }

    // Memoize your HOC if you need to
    return React.useCallback(translate);
}

export default useTranslate;
import useTranslate from "./hooks/useTranslate";

function YourComponent() {
    const __ = useTranslate();

    return (
        <p>
            {__("key1")}
        </p>
    );
}