Code Monkey home page Code Monkey logo

Comments (8)

robisim74 avatar robisim74 commented on May 23, 2024 2

Hi everyone. You must apply these instructions to the version of angular-cli that uses SystemJs. The new angular-cli@webpack version does not need to set anything. See library specification and official documentation.

from angular-l10n.

robisim74 avatar robisim74 commented on May 23, 2024 1

Ok, please note that this isn't the official Angular solution for i18n (it is not ready yet), and only for translating ng2-translate is more popular.
However, to install this library you can follow the following steps:

1 - Install angular-cli and create a new app (https://github.com/angular/angular-cli):

npm install -g angular-cli

ng new MyApp

2 - Install the library:

cd MyApp

npm install angular2localization --save

If you see some npm-warn, it's because angular-cli is not updated to Angular 2.0.0-rc.4, but it is not a problem (you can update it in package.json).

3 - Add the library to angular-cli-build.js file to vendorNpmFiles array (https://github.com/angular/angular-cli/wiki/3rd-party-libs):

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      ...
      'angular2localization/**/*.+(js|js.map)'
    ]
  });
};

4 - Configure SystemJS mappings to know where to look for the library: move to MyApp > src folder and add to system.config.js file:

/** Map relative paths to URLs. */
const map: any = {
  'angular2localization': 'vendor/angular2localization'
};

/** User packages configuration. */
const packages: any = {
  'angular2localization': { format: 'cjs', defaultExtension: 'js' }
};

A very basic usage of the library:

src > app > main.ts file:

import { bootstrap } from '@angular/platform-browser-dynamic';
import { HTTP_PROVIDERS} from '@angular/http';
import { enableProdMode } from '@angular/core';
import { AppComponent, environment } from './app/';

if (environment.production) {
  enableProdMode();
}

bootstrap(AppComponent, [HTTP_PROVIDERS]);

src > app > app.component.ts file:

import { Component } from '@angular/core';

import {
  LocaleService,
  LocalizationService,
  TranslatePipe } from 'angular2localization/angular2localization';

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
  providers: [LocaleService, LocalizationService], // Inherited by all descendants.
  pipes: [TranslatePipe]
})
export class AppComponent {

  constructor(public locale: LocaleService, public localization: LocalizationService) {

    // Adds a new language (ISO 639 two-letter or three-letter code).
    this.locale.addLanguage('en');
    // Add a new language here.

    // Required: default language and expiry (No days). If the expiry is omitted, the cookie becomes a session cookie.
    this.locale.definePreferredLanguage('en', 30);

    // DIRECT LOADING
    var translationEN = {
      TITLE: 'Angular 2 Localization with Angular command line'
    }
    // Add a new translation here.

    // Required: adds a new translation with the given language code.
    this.localization.addTranslation('en', translationEN);
    // Add a new translation with the given language code here.
    this.localization.updateTranslation(); // Need to update the translation.

  }

  // Gets the language code for the LocalizationService.
  get lang(): string {

    return this.localization.languageCode;

  }
}

src > app > app.component.html file:

<h1>
  {{ 'TITLE' | translate:lang }}
</h1>

Comment *.spec.ts files (they are used for testing).

Then run from command line:

ng build

ng serve

Wait for building and then visit: http://localhost:4200/

For more details about using of the library, look at the specifications. If you use an async loading, please note that you will need to put the json files in the ./dist folder.

I hope this helps.

Greetings

from angular-l10n.

venkatesh707 avatar venkatesh707 commented on May 23, 2024

Hi Robisim,

First i would like to thank you for spent your valuable time for this issue.Second thing you gave the steps very clearly from scratch..This works perfectly fine..I have one query..for importing this service to every component individually ,how about we import it in "main.ts" file.. and how to i keep separate files for each language instead we giving directly..

Thanks & Regards
Venky.

from angular-l10n.

robisim74 avatar robisim74 commented on May 23, 2024

Hi Venky,

Angular Team discourages the registration of a service during the bootstrap process in main.ts file as shown:

bootstrap(AppComponent, [HTTP_PROVIDERS, LocaleService, LocalizationService]); // DISCOURAGED (but works)

Everybody does it, but as we read in the official documentation:

It's just not a best practice. The bootstrap provider option is intended for configuring and overriding Angular's own preregistered services, such as its routing support.

For more information, see here.

The preferred approach is to register application providers in application components as in the example I showed you. When you do this in app.component.ts file:

@Component({
  ...
  providers: [LocaleService, LocalizationService], // Inherited by all descendants.
})
export class AppComponent {

  constructor(public locale: LocaleService, public localization: LocalizationService) {
  }
}

you are creating a reference to the services that will be inherited by all child components. In addition, if necessary, you can create new instances of the services, as in the case of lazy routing.

About your second question, the direct loading is only for example. For asynchronous loading via jsonfiles, you can follow the documentation I have already written about it: https://github.com/robisim74/angular2localization/blob/master/doc/spec.md#3.2.2. In your case, you will need to put the json files in the ./dist folder.

You also have available the sample application source code: https://github.com/robisim74/angular2localization/tree/gh-pages.

Greetings

Roberto

from angular-l10n.

AneelaBrister avatar AneelaBrister commented on May 23, 2024

Hi Roberto,

thanks for writing this as it fulfills a great need.
I installed it by using npm install angular2localization --save.
I see that the directory for it has been created under node_modules.
But when I try to use LocaleService (that's all I need for the moment), I get an Angular error: "DI exception...no provider for...LocaleService".
Then I look at the src code, and I don't see that LocaleService has an @Injectable annotation.
Any clues about why Injectable is missing -- did I miss a step?

Thanks
Aneela

from angular-l10n.

robisim74 avatar robisim74 commented on May 23, 2024

Hi Aneela,

LocaleService is injectable.

Please, check in your component if it has been added to providers:

@Component({
  ...
  providers: [LocaleService], // add here LocaleService
})

export class AppComponent {

  constructor(public locale: LocaleService) {
    ...
  }
}

In addition, if you are using angular-cli, check if the library has been copied to vendor in ./dist folder as explained above.

from angular-l10n.

zuo305 avatar zuo305 commented on May 23, 2024

Very helpful. Thanks.

from angular-l10n.

ramsunvtech avatar ramsunvtech commented on May 23, 2024

Use ng2-translate - https://github.com/ocombe/ng2-translate

Sample Project - https://github.com/ramsunvtech/angular-basic/tree/ng-2.x-cli

from angular-l10n.

Related Issues (20)

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.