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

mg-translate

v0.0.11

Published

Simple and lightweight (~3kb) angular translate library with format similar to yii or drupal

Downloads

23

Readme

mg-translate

Simple and lightweight (~3kb) angular translate library with format similar to yii or drupal

Installation

Install with Bower

$ bower install mg-translate

Install with NPM

$ npm install mg-translate

Examples

See a page on github.io as a library example

Using directive

<!-- Simple example -->
<h1 t>Hello world</h1>
<t>sample</t>

<!-- Translate an attribute -->
<input type="email" placeholder="Email" t="placeholder">

<!-- Using plural rules -->
<span t values="10">{n} Day|{n} Days</span>

<!-- Using additional variables -->
<h1 t values="{ user: 'John' }">Hello, {user}</h1>

Using filter

<h1>{{ 'Welcome' | t }}</h1>

Using service

function SampleController(t) {
    this.text = t('Welcome');
}

Load translations

Using provider config

angular
    .module('app')
    .config(function config(tProvider) {
        tProvider.load('/languages/ru.json'); // Load translations from file
        tProvider.load('ru', { // Load directly
            'Test' : 'Тест'
        });
        tProvider.load('ru', '$plural', function(n) { // Load plural rules function
            ...
        });
    });

Using service

function SampleController(t) {
    t.load('ru', { // Load directly
            'Test' : 'Тест'
        });
    t.load('/languages/ru.json').then(function() { // Load translations from file
        console.log('language is loaded');
    });
}

Load plural rules

Plural rule is just a function which gets a number and returns the corresponding index. You can get plural rules for required languages. on js-simple-plurals.

function SampleController(t) {
    t.load('ru', '$plural', function(n) { // Load plural rules function
       // function from https://github.com/megahertz/js-simple-plurals/blob/master/web/ru.js
    });
}

Load translation file

// app.config.js:
angular
    .module('app')
    .config(function config(tProvider) {
        tProvider.load('/languages/ru.json');
    });

ru.json:

{
    "language": "ru",
    "data": {
        "Welcome": "Добро пожаловать",
        "User": "Пользователь",
        …
    },
    "$plural": "function (p) { var n = Math.abs(p)||0, i = Math.floor(n,10)||0, 
    v = ((p+'').split('.')[1]||'').length, i10 = i % 10, i100 = i % 100; 
    return v === 0 && i10 === 1 && i100 !== 11 ? 0 : v === 0 && (i10 >= 2 && i10 <= 4) 
    && !(i100 >= 12 && i100 <= 14) ? 1 : v === 0 && i10 === 0 || v === 0 
    && (i10 >= 5 && i10 <= 9) || v === 0 && (i100 >= 11 && i100 <= 14) ? 2 : 3; }"
}

Load translations as a module config

(function() {
    'use strict';

    var TRANSLATIONS = {
        'Download' : 'Скачать',
        …
    };

    angular
        .module('mg.translate')
        .config(config);

    /** @ngInject */
    function config(tProvider) {
        tProvider.load('ru', '$plural', function(p) {
            // plural rule
        });

        tProvider.load('ru', TRANSLATIONS);
    }

})();

Just load this file as regular angular file

<script src="langs/ru.js"></script>

API Documentation

Provider tProvider

  • load(url:string) — Loads translation from a json file.

  • load(language:string, [category:string='app'], translations:Object) — Loads translations from Object.

  • load(language:string, '$plural', pluralRules:Function) — Loads plural rules.

  • language(language:string) — Sets an application language. If the language is not set the library tries to detect it automatically using a navigator object or a value from localStorage.lang. To disable autodetection you can set a false value.

  • language() — Returns an application language.

Service t

  • t(text:string, [null, category:string='app']) - Translates the text.

  • t(text:string, number:number, [category:string='app']) - Translates the text using plural rules. Or just replace {n} placeholder in text by the number.

  • t(text:string, values:Object, [category:string='app']) - Translates the text and replaces each placeholder by a corresponding value.

  • t.load(url:string):Promise — Loads translation from a json file

  • t.load(language:string, [category:string='app'], translations:Object) — Loads translations from Object.

  • t.load(language:string, '$plural', pluralRules:Function) — Loads plural rules.

  • t.language(language:string) — Sets an application language. If the language is not set the library tries to detect it automatically using a navigator object or a value from localStorage.lang. To disable autodetection you can set a false value.

  • t.language() — Returns an application language.

Filter t

The t filter function is the same as t() service function.

Directive t

The t directive can be used as an element or as an attribute. values and category parameters are the same as for t() service function.

<ANY
  [t="{string}"]
  [values="{number|Object}"]
  [category="{string}"]>
</ANY>

<t
  [values="{number|Object}"]
  [category="{string}"]>
</t>

Parameters

Name | Default | Description --------------------|---------|--- t (optional) | 'html' | A list of attributes which must be translated, separated by comma. The html means an element content values (optional) | null | Number for plural rules or values to replace placeholders category (optional) | 'app' | Translations category

License

Licensed under MIT.