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

metalsmith-i18next

v0.2.20

Published

Metalsmith plugin for i18next and generating your site in multiple languages.

Downloads

10

Readme

Introduction

Create multiple localised branches of your static site using the excellent i18next library.

Quick start

Installation

npm i --save metalsmith-i18next

Example

Given the following code and input folder structure, here is the output structure.

Metalsmith(__dirname)
	.use(i18next(
		pattern: '**/*.hamlc',
		locales: ['en','fr'],
		namespaces: ['public']
	))
	.build(function(err, files){
		if (err) console.error(err.stack)
	})

Input                                                   Output
-----                                                   ------
.                                                       .
|                                                       |
+- index.js                                             +- en
|                                                       |   |
+- locales                                              |   +-- index.hamlc
|   |                                                   |
|   +-- en                                              +- fr
|   |    |                                              |   |
|   |    +-- public.json                                |   +-- index.hamlc
|   |                                                   |
|   +-- fr                                              +- images
|        |                                                  |
|        +-- public.json                                    +-- smile.png
|
+- src
    |
    +-- index.hamlc
    |
    +-- images
         |
         +-- smile.png

Frontmatter

metalsmith-i18next recognizes two frontmatter properties:

  • i18nNamespace : is the name of the json file, without the extension, where the translation keys are found. This allows you to break down the files into logical groupings. The namespace can be overriden in the call to t. If no namespace is provided then first namespace in the provided options is used.

  • i18nPrefix : the prefix indicates to the tt function that the provided key should be prefixed by this value (as well as the namespace). This helps cut down on the noise in your code.

i18next Helper Functions

Once initialized, all files that match the pattern are given three helper function accessible from templates and downstream functions:

function t(String key, [Object options])

translates the key. The format for the key is 'namespace:level1.level2.level3'. If no namespace is given then the default namespace is prepended. The options can be used to override the locale with the lng option as well as to provide context to the translation function (see i18next for details).

Examples: Given the default options and namespaces ['common','public']

t('public:home.title')		// => Looks in ./locales/<locale>/public.json for home['title']
t('home.title')		        // => Looks in ./locales/<locale>/common.json for home['title']
t('title')                  // => Looks in ./locales/<locale>/common.json for title	

function tt(String key, [Object options])

specialized translation function that prepends a key before calling t. If i18nPrefix is given then it's equivalent to t.

Examples: Given the default options and i18nNamespace:'public' and i18nPrefix:'home' in the frontmatter

tt('title')		            // => Looks in ./locales/<locale>/public.json for home['title']
tt('buttons.action')        // => Looks in ./locales/<locale>/public.json for home['buttons']['action']

function tpath(String file)

translates the path of a file without locale information to one with

Examples:

// Given the default path option of ':locale/:file'
tpath('/signup.html')            // => /<locale>/signup.html
tpath('/products/widget.html')   // => /<locale>/products/widget.html

// Given a path option of ':dir/:name-:locale:ext'
tpath('/signup.html')            // => /signup-<locale>.html
tpath('/products/widget.html')   // => /products/widget--<locale>.html

License

The MIT License (MIT)

Copyright (c) 2015 Eric Methot

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.