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-translation

v0.0.3

Published

Fast, Easy and Dynamic translation for AngularJS

Downloads

6

Readme

#ngTranslation Coverage Status

Fast, Easy and Dynamic translation for AngularJS. v0.0.3

##Table of contents:

##Get Started (1) You can install ng-translation using 3 different ways:

  • clone & build this repository
  • Bower: by running $ bower install ng-translation from your terminal
  • via npm: by running $ npm install ng-translation from your terminal

(2) Include ng-translation.js (or ng-translation.min.js) in your index.html, after including Angular itself.

(3) Add 'ng-translation' to your main module's list of dependencies.

When you're done, your setup should look similar to the following:

<!doctype html>
<html ng-app="myApp">
<head>
   
</head>
<body>
    ...
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>
    <script src="bower_components/js/angular-filter.min.js"></script>
    ...
    <script>
        angular.module('myApp', ['ng-translation'])
          .controller('MainCtrl', function($scope) {
            //...
          });
    </script>
    ...
</body>
</html>

##Example Quick example:
JS:

/**
 * Directory structure
 * __ __ __ __ __ __
 * | - dist          |
 * |   - assets      |
 * |     - static    |
 * |__ __ __ __ __ __|
 */
//AngularJS app
angular.module('app', ['ng-translation'])
  .config(['ngTranslationProvider', function(ngTranslationProvider) {
    ngTranslationProvider
      .setDirectory('/dist/assets/static')
      .setFilesSuffix('.json')
      .langsFiles({
        en: 'en',
        de: 'de',
        es: 'es'
      })
      .fallbackLanguage('en')
  }])
  .run(['ngTranslation', '$location'], function(ngTranslation, $location) {
    ngTranslation.use(
        $location.search().lang
      );
  });

JSON: (one file for example)

{
  "title": "Wählen Sie eine Vorlage, um zu beginnen.",
  "description": {
    "text": "Hunderte vollständig anpassbarer HTML5-Templates in jeder Kategorie verfügbar."
  },
  "button": "Anmelden",
  "message": "Hallo {{user.name}}, Uw wachtwoord is: {{user.password}}"
}

HTML:

    <!-- simple usage example -->
    <h3>{{ 'title' | translate }}</h3>
    <h4>{{ 'description.text'  | translate }}</h4>
    <p>{{ 'button' | translate }}</p>

    <!-- directive example -->
    <p ng-translate="'message'"></p>
    <p ng-translate="es('message')"></p>

    <!-- bind to model example -->
    <table style="border:1px solid red">
        <tr>
            <td>User Details:</td>
        </tr>
        <tr>
            <td>{{ 'message' | translate: this }}</td>
        </tr>
        <tr>
            Change user details:
        </tr>
        <tr>
            <td>name: <input ng-model="user.name" type="text"/> </td>
        </tr>
        <tr>
            <td>password: <input ng-model="user.password" type="text"/> </td>
        </tr>
    </table>

To learn more, Read the documentation...

##Configuration ngTranlation configuration options, see below:

###setBaseUrl Set base url for static/languages files directory.
Aliases: setDirectory

angular.module('app', ['ng-translation'])
  .config(['ngTranslationProvider', function(ngTranslationProvider) {
    ngTranslationProvider
      .setDirectory('/ng-translation/demo/languages');
  }]);

###langsFiles Set languages files as a key value pairs.

angular.module('app', ['ng-translation'])
  .config(['ngTranslationProvider', function(ngTranslationProvider) {
    ngTranslationProvider
      .setDirectory('/ng-translation/demo/languages')
      .langsFiles({
        en: 'en.json',
        de: 'de.json',
        es: 'es.json'
      });
  }]);

###langsValues Set array of values as a languages files.

angular.module('app', ['ng-translation'])
  .value({
    en: { foo: 'Hello' },
    de: { foo: 'Hallo' }
  })
  .config(['ngTranslationProvider', function(ngTranslationProvider) {
    ngTranslationProvider
      .langsValues([
        'en',
        'de'
      ]);
  }]);

###addLangFile Add a single language file.

angular.module('app', ['ng-translation'])
  .config(['ngTranslationProvider', function(ngTranslationProvider) {
    ngTranslationProvider
      .addLangFile({
        en: 'filename.json'
      });
  }]);

###setFilesSuffix Set global suffix to all files.

angular.module('app', ['ng-translation'])
  .config(['ngTranslationProvider', function(ngTranslationProvider) {
    ngTranslationProvider
      .setFilesSuffix('-static.json')
      .langsFiles({
        en: 'en', // <== en-static.json
        de: 'de',
        es: 'es'
      });
  }]);

###fallbackLanguage Set fallback language.

angular.module('app', ['ng-translation'])
  .config(['ngTranslationProvider', function(ngTranslationProvider) {
    ngTranslationProvider
      .fallbackLanguage('en');
  }]);

##API The returns API, see below:

###configuration The expose configuration

angular.module('app', ['ng-translation'])
  .controller('MainCtrl', function(ngTranslation) {
    console.log(ngTranslation.configuration);
    //{ baseUrl: "/ng-translation/demo/languages", suffix: ".json", langsFiles: Obje... }
  });

###get Get specific file by name.
Usage: ngTranslation.get({String}) Returns: file {Object}

angular.module('app', ['ng-translation'])
  .controller('MainCtrl', function(ngTranslation) {
    $scope.getByName = function(name) {
      return ngTranslation.get(name); 
      //{title: "Select a Template", message: "Hello {{ user.name... }
    };
  });

###getAll Get all files(the staticFilesContainer) Usage: ngTranslation.getAll() Returns: files {Object}

angular.module('app', ['ng-translation'])
  .controller('MainCtrl', function(ngTranslation) {
    $scope.getAll = function() {
      return ngTranslation.getAll(); 
      //{ en: Object, de: Object, es: Obje... }
    };
  });

###getUsed Get the current used file || fallback file Usage: ngTranslation.getUsed() Returns: file {Object}

angular.module('app', ['ng-translation'])
  .controller('MainCtrl', function(ngTranslation) {
    $scope.getUsed = function() {
      return ngTranslation.getUsed(); 
       //{title: "Select a Template", message: "Hello {{ user.name... }
    };
  });

###init @private function.

###use Use specific language.(prefered language)
Usage: ngTranslation.use({String})
Returns: {Boolean}

angular.module('app', ['ng-translation'])
  .run(function($location, ngTranslation) {
    ngTranslation.use(
      $location.search().lang //e.g: "de", "en"
    );
  });

##ngTranslationFilter There's a 4 ways to use the translate filter.

  • simple - pass a key, and get the value from the usedFile(prefered language, .use).
<p>{{ 'message' | translate }}</p>
<p>{{ 'message.nested' | translate }}</p>
<!-- note: 'key' is a property on the scope -->
<p>{{ key | translate }}</p>
  • from specific file - pass a key, and fileName(language), and get the value from this file.
<p>{{ 'message' | translate: 'en' }}<p>
<p>{{ 'message.nested' | translate: 'de' }}<p>
<!-- note: 'key' and `lang` are a properties on the scope -->
<p>{{ key | translate: lang }}<p>
  • interpolate - there's a situation, that you want to store an angular expression as a value.
    e.g: 'this is string that {{ foo }}, {{ bar.baz }} need to interpolate.'
    Usage: {{ key | translate: object }}
$scope.user = { name: 'Ariel M.' }
$scope.property = 'value';
<!-- note: user is a property on the scope, so if the real value on the file is:
`hello {{ name }}, wanna login?`
the result will be: `hello Ariel M., wanna login ?` -->
<p>{{ 'user.message' | translate: user }}</p>
<!-- note: if you want to use directly properties on $scope, use the `this` keyword,
(every $scope, have the own `this` property that point to him self) -->
<p>{{ 'message' | translate: this }}</p>
  • interpolate from other file - if you want the same interpolation behavior, but get the value from specific file.
    Usage: {{ key | translate: lang: object }}
<p>{{ 'user.message' | translate: 'de': user }}</p>
<!-- note: 'key' and `lang` are a properties on the scope -->
<p>{{ key | translate: lang: this }}</p>

##ngTranslationDirective There's a 2 ways to use the ngTranslate directive.

  • get the value from the usedFile(prefered language, .use).
<p ng-translate="'message'"></p>
<!-- note: 'key' is a property on the scope -->
<p ng-translate="key"></p>
  • get the value from specific file(specific language).
<!-- note: `en` interpolate as a string -->
<p ng-translate="en('message')"></p>
<!-- note: 'key' is a property on the scope -->
<p ng-translate="de(key)"></p>