Coder Social home page Coder Social logo

angular-i8n's Introduction

Angular-i8n

It is a example of what how to make a i8n filter.

how to make an i8n(global languages) filter.

1. Make a i8n.filter.

angular
.module('i8nFilter', [])
.filter('i8n', i8nFilter);
i8nFilter.$inject = [];
function i8nFilter () {
  var map = {
    "en_US": {
      "title": "Title",
      "description": "Description",
      "parameter": "Parameter"
    },
    "ko_KR": {
      "title": "타이틀",
      "description": "소개",
      "parameter": "파라미터"
    },
    "zh_CN": {
      "title": "题目",
      "description": "介绍",
      "parameter": "参数"
    }
  };
  var lang = 'ko_KR';
  return function (text, delang) {
    if (delang) {
      lang = delang;
    }
    angular.forEach(map[lang], mappingLanguage);
    function mappingLanguage (value, key) {
      if (text.indexOf(key) != -1) {
        var reg = eval('/' + key + '/gi');  // new Reg('/' + key + '/gi');
        text = text.replace(reg, value);
      }
    }
    return text;
  };
}

2. Add the above i8n.filter on your main javascript source.

angular
.module('exampleModule', ['i8nFilter'])
.controller('exampleController', exampleController);
exampleController.$inject = ['$scope'];
function exampleController ($scope) {
}

3. Using the filter in your html.

<span>{{ 'title' | i8n }}</span>
<span>{{ 'description' | i8n }}</span>
<span>{{ 'parameter' | i8n }}</span>

NOTE

you just need to change the map, which is data, on your projects.

Developer information

email: [email protected]

angular-i8n's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.