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

ng-translator

v0.0.1

Published

A translation module for AngularJS

Downloads

3

Readme

Angular Translator (ngTranslator)

A translation module for AngularJS.

Bower version License

##Table of contents:

Getting started

Installation

You can install with bower or npm

// bower
bower install ng-translator --save

// npm
npm install ng-translator --save

Import in your project

  1. Include dist/angular-translator.min.js in our index.html, after including Angular itself.
  2. Inject ngTranslator to your main module's list of dependencies.

Configuration

In app.run method configure the translation module (This is the best way but you can do this wherever you want):

app.run(
    function($translator){
        $translator
            .configure({
                language : "en", //name of en.json
                path     : "languages/", //root directory for languages
                default  : "en" //default language
            });
    });

Define translations

Angular-translator working with external file for translations.
Create a folder in your project, for example ./languages, and put inside your translations file in json format.
For example you can create two languages (en.json and it.json) for English and Italian and should be something like:

// eng
{ "say_hi": "Hello World!" }

// it
{ "say_hi": "Ciao Mondo!" }

How to use

In the example below i use always <span> element but you can use whatever you want

Simple translation

Used for a simple text to translate

Result Hello World!

HTML

<span translated="title"></span>

en.json

{ "title" : "Hello World!" }

Translation with params

Used for translate a string that contain runtime data.
You can define your dynamic content with @param_name then use translated-params in your HTML tag for passing @param_name value.
Check the sample below:

Result A translation module for AngularJS. [v0.0.1]

HTML

<span translated="description" translated-params="version"></span>
<!-- alternative without angular var -->
<span translated="description" translated-params="{'version':'v0.0.1'}"></span>

AngularJS

$scope.version = {"version":"v0.0.1"};

en.json

{ "description": "A translation module for AngularJS. [@version]" }

Inputs

Used for translate placeholder in input filed. Insert in you translated tag placeholder-translated

HTML

<form>
    <label translated="form.username.label"></label>
    <input placeholder-translated="form.username.placehoder" translated="">
</form>

en.json

{
    "form": {
        "username": {
            "label": "Username",
            "placehoder": "Insert your username"
        },
        "submit": "Submit",
        "cancel": "Cancel"
    }
}

Translation for prural

Used for translate a string that can be single or prural.
In the example below there is a combination of parameters and prural.
In your <lang>.json file you should define an array with two elements and in position 0 the sentece for the single case and in position 1 the sentence for the prural case.

Result
1 result 6 results

HTML

<!-- Single value -->
<p translated="results" prural="results_value.one.value" translated-params="results_value.one"></p>

<!-- Prural value -->
<p translated="results" prural="6" translated-params="{'value': 6}"></p>

AngularJS

$scope.results_value = {
    "one": {
        "value" : 1
    }
};

en.json

{ "results": ["@value result", "@value results"] }

Other examples

This chapter is for understand better how to define you json file for a language.

HTML

<!-- Simple translation -->
<div translated="home_page.title"></div>

<!-- Array in <lang>.json -->
<p translated="home_page.form_inputs[0].label"></p>

<!-- Placeholder translation -->
<input translated="" placeholder-translated="home_page.form_inputs[0].placeholder">

<!-- Prural with parameters -->
<span translated="home_page.results" prural="1" translated-params="{'value': 1}"></span>

<!-- Prural with parameters -->
<h1 translated="home_page.results" prural="6" translated-params="{'value': 6}"></h1>

en.json

{
    "home_page": {
        "title" : "Home page",
        "results": [
            "@value home page result",
            "@value home page results"
        ],
        "form_inputs": [
            {
                "label": "first label input",
                "placeholder": "first input placeholder"
            }
        ]
    }
}

Dynamic translation

Inject $translator service in your controller/service/etc.. for a run time translation with $translator.translate(<key>, <params>, <prural_value>)

HTML

<button ng-click="translation()">Click Me!</button>

AngularJS

app.controller('..', ['$scope', '$translator',
    function($scope, $translator){
        $scope.translation = function(){
            alert(
                $translator.translate('message_in_controller', {param_1: "parameter"}, 1)
            );
        }
    }
);

en.json

{ "message_in_controller": "Dynamic translation!" }

Switch language

You can switch between language in real time. (try the example in this repo)

HTML

<p translated="message"></p>
<button ng-click="switch('it')">Italiano</button>
<button ng-click="switch('en')">English</button>

AngularJS

$scope.switch = function _switch(lng){
    // async method. When finish call $compile.
    $translator
        .changeLanguage({
            language : lng,
            path     : "languages/",
            default  : "en"
    });
}

en.json

{ "message" : "English message!" }

##LICENSE The MIT License

Copyright (c) 2016 Gianluca Tursi http://www.gianlucatursi.com

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.