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

site-language

v1.1.0

Published

Create multilingual sites in NodeJS with inline localisations. Edit

Downloads

6

Readme

site-language

Create multilingual sites in NodeJS with inline localisations.

What is it good for?

You may want to have an express based website that speaks two or three languages fluently. Sometime using the internalization packages is just an overkill. With this package you can define your transtaltions inline, and use them seemlessly. It's a kind of magick. ...

Prerequisites

This project requires express-session already set up and functional with a proper sessionstore.

Your best bet to install this package into your project would be:

npm install site-language --save

Another approach, in case you want to customize this script, let's say you want to put it into your app folder that contains your custom scripts.

curl https://raw.githubusercontent.com/LaKing/site-language/master/index.js > app/language.js

Installing

Once installed, tell your express app to use the public folder as source for localisations.

require('site-language')(app, 'public');
// or if you got the code in your app fodler
require('./app/language.js')(app, 'public');

Note that this command will make the files in that folder visible to the web - you don't need to use express.static on that folder.

The session will contain lang as variable. So eventually, in case you use ejs, and want your index.html to contain the language meta tag you can add it to your routes and to your index.ejs:

app.get('/', function(req, res, next) {
    res.render('index.ejs', {
        lang: req.session.lang
    });
});

<html lang="<%= lang %>">

Changing languges will be enabled by visiting the language code as uri - this will set the session.lang variable, and redirect to the site root..

    https://example.com/en
    https://example.com/hu

Usage

Once the thing is up and running, you can start to create your multilingual strings. You can do this in jour HTML, JS, JSON files, everywhere! (I suggest, to put the tags inside the strings) Let's say have a bold HTML text in your main.html file:

<b>Something as a language test.</b>

Now edit that line, and add the markup.

<b>##&en Something as a language test. ##&hu Valami teszt a nyelvek kipróbálására. ##</b>

That's it. Save it, and restart your project. Or - if you want, you can speed up developemnt by

// calling this on certain requests
language.transpile(app)

// setting this will call transpile on every / request
app.locals.settings.debug = true

How it works

When the module is initialized, the public folder is scanned, and each file is processed, that means it will be split into chunks by the ## separator, and if the chunk starts with a & character, then it is assumed that it is some text subject to localisation. For the given language code, the text is kept, all others all dropped. Simple. Once these files are rendered, they are saved in a /tmp folder and if the file is requested an express route is choosing a language based on req.session.lang, and serves the translated file.

Do I need to define my languages and my default language?

You should. Look at this to get the idea:

// defaults, in case we dont have settings
language.list = ['en', 'hu'];
language.default = 'en';

// create settings and language as objects and set these variables with some custom values
app.locals.settings.language.list
app.locals.settings.language.default

Or just edit the file if you want to have everything customized.

Deployment

Dont just drop this to a live system, I'm not sure if its bug-free. However the code is just 160 lines, and I commented it as good as possible.

Built With

ep-codepad

Contributing

Feel free to open issues.

Authors

License

This project is licensed under the ISC License

Notice

This module enables also special comments. These comments wont appear in the rendered files, thus won't be visible in browsers.

##&-- This is a comment, as it does not relate to any language ##