npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

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 🙏

© 2019 – Ryan Hefner

locizify

v2.13.0

main script for using locize service in frontend

Downloads

423

Readme

Travis Coveralls npm version Bower David

locizify

Drop the locizify script onto your website and it will automatically start to segment your content and connect it to your locize project. Translating your content was never easier.

Just drop the following line to your header to deliver your content in any language:

<script
  id="locizify"
  projectid="[PROJECT_ID]"
  apikey="[API_KEY]"
  referencelng="[LNG]"
  fallbacklng="[LNG]"
  src="https://unpkg.com/[email protected]^2.0.0"
></script>

locizify uses virtual-dom to update your page with translations based on the current content. MutationObserver is used to trigger translations on dynamically added content. So it should play well with any static or dynamic page not using a own virtual-dom implementation.

locizify comes bundled with i18next.

Getting started

Find more information

locizify wraps some other modules from locize and i18next so there are additional valuable resources to read to get details on all the provided options:

  • locize editor: ?locize=true --> readme
  • i18next language detector: ?lng=de --> readme
  • locize backend --> readme
  • i18next --> website

Like always if not finding a solution of got a question - just ping us at [email protected]

Adding locizify to your page

Add the script to your page:

<!DOCTYPE html>
<html>
  <head>
    <script
      id="locizify"
      projectid="[PROJECT_ID]"
      apikey="[API_KEY]"
      referencelng="[LNG]"
      fallbacklng="[LNG]"
      src="https://unpkg.com/[email protected]^2.0.0"
    ></script>
  </head>
  ...
</html>
  1. Reload your page.

  2. Refresh your project on locize.io - there should be added a new namespace in your reference language having all the segments of this page.

  3. Add a new language to your project and translate the content

  4. Reload your page with ?lng='[newLanguage]'

  5. Reload your page with ?locize=true to show the incontext editor to directly translate on page.

Initialize with optional options

IMPORTANT make sure you do not add your apiKey in the production build to avoid misuse by strangers

via attibutes on script element

<!DOCTYPE html>
<html>
  <head>
    <script id="locizify"
      projectid="[PROJECT_ID]"
      apikey="[API_KEY]"
      referencelng="[LNG]"
      fallbacklng="[LNG]"
      src="https://unpkg.com/[email protected]^2.0.0"

      // optional
      version="[VERSION]"
      savemissing="[true|false (default true)]"
      debug="[true|false (default false)]"
      reloadonsave="[true|false (default true)]" // automatically reload your page on saving in editor
      autopilot="[true|false (default false)]" // automatically configures fallbackLng and language whitelist

      // others
      // you can define any other option below just use the lowercased name and for arrays use comma separated values, eg.:
      ignoreclasses="ignoreMeClass1, ignoreMeClass2, ignoreMeClass3"
    ></script>
  </head>
  ...

via init function

<!DOCTYPE html>
<html>
  <head>
    <script src="https://unpkg.com/[email protected]^2.0.0"></script>
    <script>
      locizify.init({
        // required
        fallbackLng: "[LNG]",
        backend: {
          projectId: "[PROJECT_ID]",
          apiKey: "[API_KEY]", // only needed if you like to add missing segments
          referenceLng: "[LNG]",
          version: "[VERSION]", // defaults to latest
          // hostnames that are allowed to add, update keys
          // please keep those to your local system, staging, test servers (not production)
          allowedAddOrUpdateHosts: ["localhost"]
        },

        // defaults that are set
        reloadOnSave: true, // automatically reload your page on saving in editor
        autorun: true, // setting to false init will return an object with start function
        ele: document.body, // pass in another element if you like to translate another html element
        ignoreTags: ["SCRIPT"], // tags to ignore

        // using keys instead of content as keys
        keyAttr: "i18next-key", // node attribute to use as key
        ignoreWithoutKey: false, // set to true to only support nodes having a key

        // optional
        ignoreIds: ["ignoreMeId"],
        ignoreClasses: ["ignoreMeClass"],

        // attributes to translate
        translateAttributes: [
          "placeholder",
          "title",
          "alt",
          "value#input.type=button",
          "value#input.type=submit"
        ],

        // merging content (eg. a tags in p tags)
        mergeTags: [], // tags to merge innerHtml to one key
        inlineTags: [], // tags to inline (eg. a, span, abbr, ...)
        ignoreInlineOn: [], // tags to ignore inlining tags under inlineTags

        // cleanup for keys
        cleanIndent: true, // removes indent, eg. if a p tag spans multiple lines
        ignoreCleanIndentFor: ["PRE", "CODE"], // ignores cleaning up of indent for those tags needing that extra spaceing
        cleanWhitespace: true, // removes surrounding whitespace from key

        namespace: false, // set another name - default namespace will be translation
        namespaceFromPath: false, // set true will use namepace based on window.location.pathname
        ns: ["common"] // -> only set if accessing more then one namepace

        // + all options available in i18next
      });
    </script>
  </head>
  ...
</html>

Get project languages

To build some dynamic language selector you can load the available languages:

locizify.getLanguages(function(err, lngs) {
  console.warn(lngs);
});

// returns something like
{
  "en": {
    "name": "English",
    "nativeName": "English",
    "translated": {
       "latest": 1,
       "production": 1
     }
  },
  "de": {
    "name": "German",
    "nativeName": "Deutsch",
    "translated": {
       "latest": 0.8,
       "production": 1
     }
  }
}

Delay initial translation

const translation = locizify.init({
  autorun: false
});

setTimeout(function() {
  translation.start();
}, 1000);

Merge content / use html inside your translations

Just set translated attribute:

<p merge>
  all inside will be used as on segment, even if having other
  <a>elements inside</a>
</p>

// key = all inside will be used as on segment, even if having other
<a>elements inside</a>

Same could be done using options:

mergeTags: [], // tags to merge innerHtml to one key inlineTags: [], // tags to
inline (eg. a, span, abbr, ...) ignoreInlineOn: [], // tags to ignore inlining
tags under inlineTags

Fragment replacement for links and images

<img src="/images/{{a.png}}" alt="big A" />

You will find a.png to be a key in your translation files - it's value can be replaced to eg. a-de.png for german (all other languages will fallback to a.png)

<a href="/{{statistic}}">Open my statistics</a>

statistic will be a regular key that can be translated. But be aware you will need to provide that routes - eg. using localized routes on the server

Translating javascript code

You can use the i18next instance used to provide the translation functionality directly. Just make sure the instance is initialized already:

<script>
  // use t function of i18next
  // https://www.i18next.com/translation-function/essentials
  function useI18next() {
    var translated = locizify.i18next.t('some key');
  }

  if (locizify.i18next.isInitialized) {
    useI18next();
  } else {
    locizify.i18next.on('initialized', function(options) {
      useI18next();
    })
  }
</script>

Avoid translating an element

By attribute

Just set translated attribute:

<div translated>this will not get translated - nor this elements children</div>
By ignoring tag, class, id

Just add needed items to the specific array:

locizify.init({
  ignoreTags: ["SCRIPT"], // need to be uppercased
  ignoreIds: ["ignoreMeId"],
  ignoreClasses: ["ignoreMeClass"]
});
<script>
  this will not get translated - nor this elements children
</script>
<div id="ignoreMeId">
  this will not get translated - nor this elements children
</div>
<div class="ignoreMeClass">
  this will not get translated - nor this elements children
</div>

Just add translated-attribute

Advanced Translation Features

For advanced translations like plurals, interpolation, ... you need to add options to the element

Interpolation

<div i18next-options='{"foo": "bar"}'>
  foo {{bar}}
  <p i18next-options='{"foo2": "bar2"}'>foo {{foo}}; foo2 {{foo2}}</p>
</div>

Options get inherited from parent to child nodes.

Plural

<p i18next-options='{"count": 2}'>plural {{count}} items</p>

Set different namespaces

Default would be translation.

Set a different one:

locizify.init({
  namespace: "myNamespace"
});

autogenerate one per route:

locizify.init({
  namespaceFromPath: true
});

Access different namespaces

This is useful for reused elements that are on every page, eg. like footer,... and you're using namespaceFromPath. This way you can avoid having that segments on every routes namespace file.

locizify.init({
  namespaceFromPath: true
  ns: ['common'] // -> add additional namespaces to load
});
<div i18next-options='{"ns": "common"}'>
  <p>different namespace common is used</p>
  <p>all the way down</p>
</div>

Avoid flickering on initial load

To avoid to show the user the untranslated content in the reference language you can:

<!DOCTYPE html>
<html>
  <head>
    ...
  </head>
  <body style="display: none">
    ...
  </body>
</html>

Just set the element style display to none. locizify will change it to block when ready.

Change namespace dynamically per code

You can change the namespace after loading to some other file (eg. before transitioning to another page).

locizify.changeNamespace("newNamespace");

Specify own keys for the translation segments

Per default the virtualdom implementation will use the extracted content as keys leading to translation JSON in the form:

// <div>This is my text.</div>
// <a href="#" title="My Link Title" >My Link</a>
{
  "This is my text.": "This is my text.",
  "My Link": "My Link",
  "My Link Title": "My Link Title"
}

In some situations you like to define an explicit key for that segment:

// <div i18next-key="myKey">This is my text.</div>
// <a i18next-key="myLink" href="#" title="My Link Title" >My Link</a>
{
  "myKey": "This is my text.",
  "myLink": "My Link",
  "myLink.title": "My Link Title"
}

There are two init options to configure this further:

locizify.init({
  // ...

  keyAttr: "i18next-key", // node attribute to use as key
  ignoreWithoutKey: false // set to true to only extract/translate nodes having a key

  // ...
});

Setting ignoreWithoutKey: true is recommended when using keys to get a clean key-based result.

Important The locizify script does not support nested JSON structures like:

{
  "myKey": {
    "attr1": "value",
    "attr2": "value"
  }
}

You will have to enforce flat JSON in your locize project (settings -> publish format -> JSON flat)

{
  "myKey.attr1": "value",
  "myKey.attr2": "value"
}