Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to setup external library with ng2admin (like ng2translate) #8

Closed
sanathpathiraja opened this issue May 31, 2016 · 4 comments
Closed

Comments

@sanathpathiraja
Copy link

getting error,
browser_adapter.ts?9397:78 EXCEPTION: Error: Uncaught (in promise): Template parse errors:
The pipe 'translate' could not be found

i added
app.component.ts
import {TRANSLATE_PROVIDERS, TranslateService, TranslatePipe, TranslateLoader, TranslateStaticLoader} from 'ng2-translate/ng2-translate';

@component({
selector: 'app',
pipes: [TranslatePipe],

but errors comes.

@nnixaa
Copy link
Collaborator

nnixaa commented May 31, 2016

@sanathpathiraja if you want to globally include translate pipe, add it to APPLICATION_PIPES array in src\platform\browser\pipes.ts. In this case, you won't need to import and explicitly register the pipe in each component.

@nnixaa nnixaa closed this as completed May 31, 2016
@sanathpathiraja
Copy link
Author

yes i did that.

app/index.ts
// App
import {AppState} from "./app.state";
import {TRANSLATE_PROVIDERS,
TranslateService,
TranslateLoader,
TranslateStaticLoader} from 'ng2-translate/ng2-translate';
import {provide} from '@angular/core';
import {Http} from '@angular/http';
export * from './app.component';

// Application wide providers
export const APP_PROVIDERS = [
AppState,
provide(TranslateLoader, {
useFactory: (http: Http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'),
deps: [Http]
}),
TRANSLATE_PROVIDERS,
TranslateService
];

app.component.ts
import {TRANSLATE_PROVIDERS, TranslateService, TranslatePipe, TranslateLoader, TranslateStaticLoader} from 'ng2-translate/ng2-translate';
export class App {

isMenuCollapsed:boolean = false;
constructor(private _state:AppState, private _imageLoader:BaImageLoaderService, private _spinner:BaThemeSpinner, translate: TranslateService) {
translate.setDefaultLang('en');

but translate not working.
can you help me?

@nnixaa
Copy link
Collaborator

nnixaa commented May 31, 2016

@sanathpathiraja still you haven't loaded the pipe globally, here the minimal steps:

  1. install the module
  2. in src/platform/broswers/pipes.ts add translate pipe like this:
import {TranslatePipe} from 'ng2-translate/ng2-translate';

// application_pipes: pipes that are global through out the application
export const APPLICATION_PIPES = [
  TranslatePipe // <----- add the pipe HERE
];

// the rest of the code
  1. in src/platform/broswers/providers.ts add TRANSLATE_PROVIDERS like this:
import {TRANSLATE_PROVIDERS} from 'ng2-translate/ng2-translate';

/*
* Application Providers/Directives/Pipes
* providers/directives/pipes that only live in our browser environment
*/
export const APPLICATION_PROVIDERS = [
  ...FORM_PROVIDERS,
  ...HTTP_PROVIDERS,
  ...ROUTER_PROVIDERS,
  TRANSLATE_PROVIDERS, // <--- HERE the providers
  {provide: LocationStrategy, useClass: HashLocationStrategy }
];

// the rest of the code
  1. now use it in any template you need to

@sanathpathiraja
Copy link
Author

yes, I added that after I change
src/main.browser.js

`export function main(initialHmrState?: any): Promise {

return bootstrap(App, [
...PROVIDERS,
...ENV_PROVIDERS,
...DIRECTIVES,
...PIPES,
...APP_PROVIDERS,
HTTP_PROVIDERS,
provide(TranslateLoader, {
useFactory: (http: Http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'),
deps: [Http]
}),
// use TranslateService here, and not TRANSLATE_PROVIDERS (which will define a default TranslateStaticLoader)
TranslateService
])
.catch(err => console.error(err));`

after run it perfectly.
thanks @nnixaa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants