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

@pubcore/ui-text

v1.1.3

Published

map textkeys to text

Downloads

6

Readme

Localize text via text-key to text-value objects

Whenever we code interfaces for human users it is very likely we need to show some text. An example of a simple React based component with some "Hello World" text may look like this:

import React from 'react'

export default function Dialog(){
	return <h1>Hello World</h2>
}

If we have to support more than one language and don't want to duplicate interface's code, this library can be used. It is build to to create a text-function initialzed and configured in application's global context. The text-function is imported where ever a text is needed. So instead of hard code a text, a text-key string "title" is used. (irrelevant codelines are replace with "[..]"):

[..]
import T from '../lib/text.mjs'

[..]
	return <h1>{ T('title') }</h2>
[..]

If we assume the langauge is a global setting (which is the case for most interfaces) we define the text-function bound to current language's text object, in this example within the "text.mjs" file:

Initialization and creation of the text-function (global scope)

import uiText from '@pubcore/ui-text'

//example text data, it may come from a content management system
//where texts are translated and maintained
const text = {
	title: 'Hello World'
}

export default (...args) => uiText(text, ...args)

Code examples

Text with some replacement
<h1>{ T('title', 'Hello World! #{count} example', {count:2}) }</h1>
//result: <h1>Hello World! #2 example</h1>
Shortcut for short and generic meaning text

If key contains whitespaces, it will be transformed to snake case, here key will be "sort_descent"

<item>{ T('Sort descent') }</item>
//result: <item>Sort descent</item>

This will lead to text-key "cancel" (text-keys are snake-case)

<button>{ T('Cancel') }</button>
//result: <button>Cancel</button>
Optional text

Optional text may not be defined in language-text-object and therefore it will not trigger errors or fall back to any default text. If a default text is not defined, an empty string is returned. This is especially usefull if dynamic text-keys are created having a static prefix and some dynamic postfix:

//a text is optional, if it's text-key is defined as array
var color = 'blue'
[..]
<div>{ T(['additional_info_', color]) }</div>

Options

Per default the text function does work in production mode. If set to development mode the behaviour changes: In development mode

  • registerDefaultText callback is called (if configured) which can be used to auto-save, register or alert new or missing text-values for requested keys. A text-key is treated as new, if it does not exists in language-text-object.
  • An Error is thrown, if no default text is defined for a called text-key
import uiText from '@pubcore/ui-text'

[..]
const options = {
	envType:'development', //default: production
	defaultTexts:{}, //text-key-value object
	registerDefaultText: (text) => {} //callback function
}

export default (...args) => uiText(text, ...args, options)