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

@doubco/world

v1.3.2

Published

Lightweight internationalization library for JS.

Downloads

60

Readme

World 🌎

Lightweight internationalization library for browser, nodejs, react and nextjs.

Install

npm install @doubco/world --save or yarn add @doubco/world

Usage

Simple

import { World } from "@doubco/world";

export default new World({
  locale: "en",
  defaultLocale: "en",
  translations: {
    en: {
      hello: "Hello",
      item: "Item",
      item_plural: "Items",
      "x-selected": "{{count}} Selected Item {{by}}",
      "x-selected_plural": "{{count}} Selected Items {{by}}",
      yell: "Yell as {{name,uppercase}}",
    },
    tr: {
      hello: "Merhaba!",
      item: "Madde",
      item_plural: "Madde",
      "x-selected": "{{by}} tarafından {{count}} Seçili",
      "x-selected_plural": "{{by}} tarafından {{count}} Seçili",
      yell: "{{name,uppercase}} diye bağır!",
    },
  },
  onLocaleChange: (locale, callback) => {
    // do your stuff with locale

    // don't forget to call the callback
    if (callback) callback();
  },
  formatter: ({ method, value, locale, options }) => {
    // do your stuff in here
    if (method == "uppercase") return value.toLocaleUpperCase(locale);
    return value;
  },
});

any.js

import __ from "./world";

export const translatedTitle = () => {
  return __.t("x-selected", { count: 2, by: "Berkay" });
  // returns 1 Selected Items by Berkay
};

export const switchLanguage = () => {
  return __.setLocale(
    "en",
    callback /* will run after locale and onLocalChange*/,
    dontFetch /* if true, won't trigger fetch*/,
  );
};

Fetch translations

import { World } from "@doubco/world";

export default new World({
  locale: "en",
  defaultLocale: "en",
  fetch: (locale) => {
    // return a promise
    // translations == { en: {key:"value"}, tr: {key:"value"}}
    return new Promise((resolve) => {
      fetch(url)
        .then((translations = {}) => {
          resolve(translations);
        })
        .catch((e) => {
          resolve({});
        });
    });
  },
});

Register translations

import WorldInstance from "./world";

WorldInstance.registerTranslation("en", { hello: "Hello" });
WorldInstance.registerTranslations({
  en: { hello: "Hello" },
  tr: { hello: "Merhaba" },
});

Usage with React

App.js

import WorldInstance from "./world";

const App = () => {
  return (
    <WorldProvider instance={WorldInstance}>
      <MyApp {...this.props} />
    </WorldProvider>
  );
};

export default App;

MyComponent.js

import { withWorld } from "@doubco/world/lib/react";

class MyComponent extends React.Component {
  render() {
    const { t, world } = this.props;
    return (
      <div>
        {t("hello")} | {world.locale}
      </div>
    );
  }
}

export default withWorld({})(MyComponent);

Usage with Next.js (SSR)

_app.js

import React from "react";
import App, { Container } from "next/app";

import WorldInstance from "./world";

export default class MyApp extends App {
  static async getInitialProps({ Component, ctx }) {
    let pageProps = {};

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }

    const locale = "en"; // or get it from cookies.

    const worldContext = await WorldInstance.createContext(locale);

    return { pageProps, worldContext };
  }
  constructor(props) {
    super(props);

    const { worldContext } = this.props;

    if (worldContext) {
      WorldInstance.registerContext(worldContext);
    }
  }
  render() {
    const { Component, pageProps } = this.props;

    return (
      <Container>
        <Component {...pageProps} />
      </Container>
    );
  }
}

Notes

For now only _plural rule supported. It will work on most use cases, if you need anything else please open an issue or create a pull request.


Contribute

Pull requests are welcome and please submit bugs 🐛.

Contact