Comments (8)
I am not sure what you mean with local static file in context of webdevelopment. Every file which wil be used from a setup needs to be delivered via http. The static file loader will send a request for the translation file to
http://yourdomain.com/{{prefix}}/lang{{suffix}}
If you mean you want to load the translation file from another host then this should also be possible. There you would need to include the url of the host into the prefix.
from core.
Thank you Daniel,
I could not get the resource file I wanted because I assumed (without checking it) that I can set the suffix
as an empty string.
Have a nice day!
from core.
The module is still trying to get the translation file from http://192.168.2.2:5555/i18n/en.json
and that returns 404 despite the fact I specified another location, here is the snippet
var prefix = 'http://192.168.2.2:1234/api/v1/localisation/resources';
var suffix = '.json';
translate.useStaticFilesLoader(prefix, suffix);
any ideas why the prefix is being ignored?
I'm using the latest ng2-translate module - 1.7.3
from core.
Hmm no reason, it should work with this prefix & suffix :-/
Are you sure that you're using the same instance of TranslateService ? You need to define it in bootstrap and never define it in the providers
property of a component, otherwise it will create a new instance each time.
from core.
OK, one step closer :)
The app makes 2 requests for the en.js , as follows:
http://192.168.2.2:5555/i18n/en.json
- 404
http://192.168.2.2:1234/api/v1/localisation/resources/en.json
- 200
I've registered the TranslateService in bootstrap file and then in the root component, as follows:
main.ts
import {provide, enableProdMode} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS, APP_BASE_HREF} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';
import {AppComponent} from './app/components/app.component';
import {TranslateService} from 'ng2-translate/ng2-translate';
import 'rxjs/Rx'; // this would import all RxJS operators
if ('<%= ENV %>' === 'prod') { enableProdMode(); }
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
provide(APP_BASE_HREF, { useValue: '<%= APP_BASE %>' }),
HTTP_PROVIDERS,
TranslateService
]);
app.ts
import {Component, ViewEncapsulation} from 'angular2/core';
import {ROUTER_DIRECTIVES, RouteConfig} from 'angular2/router';
import {TranslateService} from 'ng2-translate/ng2-translate';
@Component({
selector: 'sd-app',
moduleId: module.id,
templateUrl: './app.component.html',
encapsulation: ViewEncapsulation.None,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{ path: '/', name: 'Home', component: HomeComponent }
])
export class AppComponent {
constructor(public translate: TranslateService) {
//set the app language
var appLang ='en';
var prefix = 'http://192.168.2.2:1234/api/v1/localisation/resources';
var suffix = '.json';
translate.useStaticFilesLoader(prefix, suffix);
var userLang = navigator.language.split('-')[0]; // use navigator lang if available
userLang = /(fr|en)/gi.test(userLang) ? userLang : appLang;
translate.setDefaultLang('en');
translate.use(userLang);
}
}
from core.
Yes it looks correct.
I'm gonna try to reproduce it and let you know.
from core.
Hi Olivier
You were right, I had another service which was using it, however I was expecting the TranslateService to be injected from DI because it was already registered in main bootstrap file.
import {Injectable} from 'angular2/core';
import {TranslateService} from 'ng2-translate/ng2-translate';
@Injectable()
export class LanguageService {
//private languages = ['en', 'fr'];
private currentLang:string;
constructor(private translate: TranslateService) {
if (localStorage.getItem('lang') !== null) {
this.currentLang = <string>JSON.parse(localStorage.getItem('lang'));
translate.use(this.currentLang );//transaltion module
console.log('Reading Lang from the LocalStorage');
} else {
//default languages
this.currentLang= 'en';
localStorage.setItem('lang', this.currentLang);
translate.use(this.currentLang );//transaltion module
console.log('Lang not found in the LocalStorage, setting a default to: en');
}
}
get(): string {
return this.currentLang;
}
set(value: string): void {
this.currentLang= value;
//save
localStorage.setItem('lang', this.currentLang);
this.translate.use(value);// I ASSUME THIS LINE IS CREATING ANOTHER INSTANCE OF THE TRANSLATE SERVICE?
console.log('Saving cart to LocalStorage');
}
}
Thank you very much for your help!
from core.
Ok good to know !
I should try to find a way to have a global configuration for all instances, because that's probably a problem that other will have
from core.
Related Issues (20)
- InnerText gets repeated/duplicated when translate directive being used
- Translations mixed in app between two languages.
- How to handle import TranslateModule.forChild in module when moving to standalone components HOT 2
- reuse last loaded loder in standalone component which doesn't load by routes
- setTranslation is confusing the translation
- bump angular 17 HOT 3
- Translation keys are ignored when dot format and nested format are mixed
- How to handle import TranslateModule.forChild in module when creating a standalone component in an NgModule Application HOT 8
- How to use ngx-translate in Angular 17, Knowing that AppModule is not implemented? HOT 2
- Support for Angular v17 HOT 27
- ngx-translate creates billions of tiny files HOT 1
- Using ngx-translate in a standalone component HOT 3
- 'EventEmitter' and 'DefaultLangChangeEvent' is declared but its value is never read.
- Angular 17 SSR. Standalone implementation - NullInjectorError: No provider for _TranslatePipe! HOT 1
- I am trying to add Arabic language. I have configured package correctly but some labels are translated some are not
- Latency when loading the website, because Translation Key appears when user opens our website HOT 1
- Add opption to pass callback in translate pipe
- ncaught SyntaxError: Cannot use import statement outside a module (at scripts.js:16887:1) main.ts:13 Error: Unexpected value 'TranslateModule' imported by the module 'AppModule'. Please add an @NgModule annotation. at verifySemanticsOfNgModuleDef (core.mjs:28868:5) at core.mjs:28878:5 at Array.forEach (<anonymous>) at verifySemanticsOfNgModuleDef (core.mjs:28876:43) at AppModule.get (core.mjs:28833:23) at getInjectorDef (core.mjs:333:1) at walkProviderTree (core.mjs:5550:1) at core.mjs:5500:6 at core.mjs:4701:1 at Array.forEach (<anonymous>)
- Wrong TranslateHttpLoader after upgrade app to Angular 17
- ngx-translate not working with interceptor in Ionic Capacitor app
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.