how to use TranslateHttpLoader with service url in ngx-translate
I am trying to get translated text from content management system with service URL. When I use a JSON file it works well, but how can I use a service URL to get translated data?
This is the code that's not working:
export function createTranslateLoader(http: Http) {
return new TranslateHttpLoader(http, 'http://test.test.com/test/test/Translations/{lang}/{SiteId}');
}
javascript angular
add a comment |
I am trying to get translated text from content management system with service URL. When I use a JSON file it works well, but how can I use a service URL to get translated data?
This is the code that's not working:
export function createTranslateLoader(http: Http) {
return new TranslateHttpLoader(http, 'http://test.test.com/test/test/Translations/{lang}/{SiteId}');
}
javascript angular
Please format your question
– Jota.Toledo
Jun 21 '17 at 9:07
It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please edit your question to give a more complete description of what you expected to happen and how that differs from the actual results. See How to Ask for hints on what makes a good explanation.
– Toby Speight
Jun 21 '17 at 10:39
add a comment |
I am trying to get translated text from content management system with service URL. When I use a JSON file it works well, but how can I use a service URL to get translated data?
This is the code that's not working:
export function createTranslateLoader(http: Http) {
return new TranslateHttpLoader(http, 'http://test.test.com/test/test/Translations/{lang}/{SiteId}');
}
javascript angular
I am trying to get translated text from content management system with service URL. When I use a JSON file it works well, but how can I use a service URL to get translated data?
This is the code that's not working:
export function createTranslateLoader(http: Http) {
return new TranslateHttpLoader(http, 'http://test.test.com/test/test/Translations/{lang}/{SiteId}');
}
javascript angular
javascript angular
edited Jun 21 '17 at 10:39
Toby Speight
16.4k133965
16.4k133965
asked Jun 21 '17 at 8:58
BegumBegum
63
63
Please format your question
– Jota.Toledo
Jun 21 '17 at 9:07
It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please edit your question to give a more complete description of what you expected to happen and how that differs from the actual results. See How to Ask for hints on what makes a good explanation.
– Toby Speight
Jun 21 '17 at 10:39
add a comment |
Please format your question
– Jota.Toledo
Jun 21 '17 at 9:07
It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please edit your question to give a more complete description of what you expected to happen and how that differs from the actual results. See How to Ask for hints on what makes a good explanation.
– Toby Speight
Jun 21 '17 at 10:39
Please format your question
– Jota.Toledo
Jun 21 '17 at 9:07
Please format your question
– Jota.Toledo
Jun 21 '17 at 9:07
It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please edit your question to give a more complete description of what you expected to happen and how that differs from the actual results. See How to Ask for hints on what makes a good explanation.
– Toby Speight
Jun 21 '17 at 10:39
It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please edit your question to give a more complete description of what you expected to happen and how that differs from the actual results. See How to Ask for hints on what makes a good explanation.
– Toby Speight
Jun 21 '17 at 10:39
add a comment |
2 Answers
2
active
oldest
votes
Yesterday I have faced the same issue on how to make use of JSON response from API calls using ngx-translate and I have identified the solution. I know its very late to update here, but I believe some one will get benefit out of it.
Ngx-translate offers to use Service if you are not going to use .json file. For this, in your *.module.ts file, "useClass" instead of "useFactory" like below.
@NgModule({
imports: [SharedModule, SharedComponentsModule, routing,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslationService,
//useFactory : HttpLoaderFactory,
deps: [HttpClient]
}
In your Translation Service, just make call of API url something like this.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { TranslateLoader } from '@ngx-translate/core';
@Injectable()
export class TranslationService implements TranslateLoader {
constructor(private http: HttpClient) { }
getTranslation(lang: string): Observable<any> {
return this.http.get(`http://<someurlreferences>?language=${lang}`)
.map((response: JSON) => {
return response;
});
}
}
Remember: The API response should be in JSON format. Else, ngx-translate would not translate.
Assuming your JSON response is in below Format
{
"Data": {
"First": [{
"key": "FirstKey",
"value": "FirstValue"
}]
}
}
Then, in your *.component.ts file
import { Component, OnInit } from '@angular/core'
import { TranslateService } from '@ngx-translate/core';
@Component({
template: `<button (click)="switchLanguage('en')" >English</button>
<button (click)="switchLanguage('cy')" >Welsh</button>
<h1>{{'Data.First.0.value' | translate }} <!--FirstValue-->
</h1>`
export class SomeComponent implements OnInit {
constructor(private translate: TranslateService) {
translate.addLangs(['en', 'cy']);
translate.setDefaultLang('en');
const browserLang = translate.getBrowserLang();
translate.use(browserLang.match(/en|cy/) ? browserLang : 'en');
}
switchLanguage(language: string) {
this.translate.use(language);
}
}
The UI displays two buttons (English and Welsh) and on clicking, the service API url will be called based on language parameter accordingly.
add a comment |
I ran into the same issue when trying to move from 'assets/i18n/english.json' etc to MySQL database for fetching the translated values dynamically from a table. I wanted to do this so that the admin can change them on his own.
So, I tried the answer given by Viswa and it works but I found a simpler way of doing this HttpLoaderFactory. Just add the following to your app.module.ts. Replace the URL with your API. Mine was like 'http://localhost:8080/translation/:language'
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http, "http://localhost:8080/translation/", "");
}
Another thing that wasted some time as I was sending an array of objects like [{name: 'Name'}]
instead of sending just an object like {name: 'Name'}
. Once that was fixed, it worked like a charm!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44671412%2fhow-to-use-translatehttploader-with-service-url-in-ngx-translate%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yesterday I have faced the same issue on how to make use of JSON response from API calls using ngx-translate and I have identified the solution. I know its very late to update here, but I believe some one will get benefit out of it.
Ngx-translate offers to use Service if you are not going to use .json file. For this, in your *.module.ts file, "useClass" instead of "useFactory" like below.
@NgModule({
imports: [SharedModule, SharedComponentsModule, routing,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslationService,
//useFactory : HttpLoaderFactory,
deps: [HttpClient]
}
In your Translation Service, just make call of API url something like this.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { TranslateLoader } from '@ngx-translate/core';
@Injectable()
export class TranslationService implements TranslateLoader {
constructor(private http: HttpClient) { }
getTranslation(lang: string): Observable<any> {
return this.http.get(`http://<someurlreferences>?language=${lang}`)
.map((response: JSON) => {
return response;
});
}
}
Remember: The API response should be in JSON format. Else, ngx-translate would not translate.
Assuming your JSON response is in below Format
{
"Data": {
"First": [{
"key": "FirstKey",
"value": "FirstValue"
}]
}
}
Then, in your *.component.ts file
import { Component, OnInit } from '@angular/core'
import { TranslateService } from '@ngx-translate/core';
@Component({
template: `<button (click)="switchLanguage('en')" >English</button>
<button (click)="switchLanguage('cy')" >Welsh</button>
<h1>{{'Data.First.0.value' | translate }} <!--FirstValue-->
</h1>`
export class SomeComponent implements OnInit {
constructor(private translate: TranslateService) {
translate.addLangs(['en', 'cy']);
translate.setDefaultLang('en');
const browserLang = translate.getBrowserLang();
translate.use(browserLang.match(/en|cy/) ? browserLang : 'en');
}
switchLanguage(language: string) {
this.translate.use(language);
}
}
The UI displays two buttons (English and Welsh) and on clicking, the service API url will be called based on language parameter accordingly.
add a comment |
Yesterday I have faced the same issue on how to make use of JSON response from API calls using ngx-translate and I have identified the solution. I know its very late to update here, but I believe some one will get benefit out of it.
Ngx-translate offers to use Service if you are not going to use .json file. For this, in your *.module.ts file, "useClass" instead of "useFactory" like below.
@NgModule({
imports: [SharedModule, SharedComponentsModule, routing,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslationService,
//useFactory : HttpLoaderFactory,
deps: [HttpClient]
}
In your Translation Service, just make call of API url something like this.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { TranslateLoader } from '@ngx-translate/core';
@Injectable()
export class TranslationService implements TranslateLoader {
constructor(private http: HttpClient) { }
getTranslation(lang: string): Observable<any> {
return this.http.get(`http://<someurlreferences>?language=${lang}`)
.map((response: JSON) => {
return response;
});
}
}
Remember: The API response should be in JSON format. Else, ngx-translate would not translate.
Assuming your JSON response is in below Format
{
"Data": {
"First": [{
"key": "FirstKey",
"value": "FirstValue"
}]
}
}
Then, in your *.component.ts file
import { Component, OnInit } from '@angular/core'
import { TranslateService } from '@ngx-translate/core';
@Component({
template: `<button (click)="switchLanguage('en')" >English</button>
<button (click)="switchLanguage('cy')" >Welsh</button>
<h1>{{'Data.First.0.value' | translate }} <!--FirstValue-->
</h1>`
export class SomeComponent implements OnInit {
constructor(private translate: TranslateService) {
translate.addLangs(['en', 'cy']);
translate.setDefaultLang('en');
const browserLang = translate.getBrowserLang();
translate.use(browserLang.match(/en|cy/) ? browserLang : 'en');
}
switchLanguage(language: string) {
this.translate.use(language);
}
}
The UI displays two buttons (English and Welsh) and on clicking, the service API url will be called based on language parameter accordingly.
add a comment |
Yesterday I have faced the same issue on how to make use of JSON response from API calls using ngx-translate and I have identified the solution. I know its very late to update here, but I believe some one will get benefit out of it.
Ngx-translate offers to use Service if you are not going to use .json file. For this, in your *.module.ts file, "useClass" instead of "useFactory" like below.
@NgModule({
imports: [SharedModule, SharedComponentsModule, routing,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslationService,
//useFactory : HttpLoaderFactory,
deps: [HttpClient]
}
In your Translation Service, just make call of API url something like this.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { TranslateLoader } from '@ngx-translate/core';
@Injectable()
export class TranslationService implements TranslateLoader {
constructor(private http: HttpClient) { }
getTranslation(lang: string): Observable<any> {
return this.http.get(`http://<someurlreferences>?language=${lang}`)
.map((response: JSON) => {
return response;
});
}
}
Remember: The API response should be in JSON format. Else, ngx-translate would not translate.
Assuming your JSON response is in below Format
{
"Data": {
"First": [{
"key": "FirstKey",
"value": "FirstValue"
}]
}
}
Then, in your *.component.ts file
import { Component, OnInit } from '@angular/core'
import { TranslateService } from '@ngx-translate/core';
@Component({
template: `<button (click)="switchLanguage('en')" >English</button>
<button (click)="switchLanguage('cy')" >Welsh</button>
<h1>{{'Data.First.0.value' | translate }} <!--FirstValue-->
</h1>`
export class SomeComponent implements OnInit {
constructor(private translate: TranslateService) {
translate.addLangs(['en', 'cy']);
translate.setDefaultLang('en');
const browserLang = translate.getBrowserLang();
translate.use(browserLang.match(/en|cy/) ? browserLang : 'en');
}
switchLanguage(language: string) {
this.translate.use(language);
}
}
The UI displays two buttons (English and Welsh) and on clicking, the service API url will be called based on language parameter accordingly.
Yesterday I have faced the same issue on how to make use of JSON response from API calls using ngx-translate and I have identified the solution. I know its very late to update here, but I believe some one will get benefit out of it.
Ngx-translate offers to use Service if you are not going to use .json file. For this, in your *.module.ts file, "useClass" instead of "useFactory" like below.
@NgModule({
imports: [SharedModule, SharedComponentsModule, routing,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslationService,
//useFactory : HttpLoaderFactory,
deps: [HttpClient]
}
In your Translation Service, just make call of API url something like this.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { TranslateLoader } from '@ngx-translate/core';
@Injectable()
export class TranslationService implements TranslateLoader {
constructor(private http: HttpClient) { }
getTranslation(lang: string): Observable<any> {
return this.http.get(`http://<someurlreferences>?language=${lang}`)
.map((response: JSON) => {
return response;
});
}
}
Remember: The API response should be in JSON format. Else, ngx-translate would not translate.
Assuming your JSON response is in below Format
{
"Data": {
"First": [{
"key": "FirstKey",
"value": "FirstValue"
}]
}
}
Then, in your *.component.ts file
import { Component, OnInit } from '@angular/core'
import { TranslateService } from '@ngx-translate/core';
@Component({
template: `<button (click)="switchLanguage('en')" >English</button>
<button (click)="switchLanguage('cy')" >Welsh</button>
<h1>{{'Data.First.0.value' | translate }} <!--FirstValue-->
</h1>`
export class SomeComponent implements OnInit {
constructor(private translate: TranslateService) {
translate.addLangs(['en', 'cy']);
translate.setDefaultLang('en');
const browserLang = translate.getBrowserLang();
translate.use(browserLang.match(/en|cy/) ? browserLang : 'en');
}
switchLanguage(language: string) {
this.translate.use(language);
}
}
The UI displays two buttons (English and Welsh) and on clicking, the service API url will be called based on language parameter accordingly.
edited Jul 1 '18 at 20:31
halfer
14.4k758109
14.4k758109
answered Jun 13 '18 at 5:30
ViswaViswa
3419
3419
add a comment |
add a comment |
I ran into the same issue when trying to move from 'assets/i18n/english.json' etc to MySQL database for fetching the translated values dynamically from a table. I wanted to do this so that the admin can change them on his own.
So, I tried the answer given by Viswa and it works but I found a simpler way of doing this HttpLoaderFactory. Just add the following to your app.module.ts. Replace the URL with your API. Mine was like 'http://localhost:8080/translation/:language'
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http, "http://localhost:8080/translation/", "");
}
Another thing that wasted some time as I was sending an array of objects like [{name: 'Name'}]
instead of sending just an object like {name: 'Name'}
. Once that was fixed, it worked like a charm!
add a comment |
I ran into the same issue when trying to move from 'assets/i18n/english.json' etc to MySQL database for fetching the translated values dynamically from a table. I wanted to do this so that the admin can change them on his own.
So, I tried the answer given by Viswa and it works but I found a simpler way of doing this HttpLoaderFactory. Just add the following to your app.module.ts. Replace the URL with your API. Mine was like 'http://localhost:8080/translation/:language'
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http, "http://localhost:8080/translation/", "");
}
Another thing that wasted some time as I was sending an array of objects like [{name: 'Name'}]
instead of sending just an object like {name: 'Name'}
. Once that was fixed, it worked like a charm!
add a comment |
I ran into the same issue when trying to move from 'assets/i18n/english.json' etc to MySQL database for fetching the translated values dynamically from a table. I wanted to do this so that the admin can change them on his own.
So, I tried the answer given by Viswa and it works but I found a simpler way of doing this HttpLoaderFactory. Just add the following to your app.module.ts. Replace the URL with your API. Mine was like 'http://localhost:8080/translation/:language'
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http, "http://localhost:8080/translation/", "");
}
Another thing that wasted some time as I was sending an array of objects like [{name: 'Name'}]
instead of sending just an object like {name: 'Name'}
. Once that was fixed, it worked like a charm!
I ran into the same issue when trying to move from 'assets/i18n/english.json' etc to MySQL database for fetching the translated values dynamically from a table. I wanted to do this so that the admin can change them on his own.
So, I tried the answer given by Viswa and it works but I found a simpler way of doing this HttpLoaderFactory. Just add the following to your app.module.ts. Replace the URL with your API. Mine was like 'http://localhost:8080/translation/:language'
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http, "http://localhost:8080/translation/", "");
}
Another thing that wasted some time as I was sending an array of objects like [{name: 'Name'}]
instead of sending just an object like {name: 'Name'}
. Once that was fixed, it worked like a charm!
edited Dec 28 '18 at 14:34
Roman Skydan
7061829
7061829
answered Dec 28 '18 at 12:52
Bharat Raj SayaBharat Raj Saya
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44671412%2fhow-to-use-translatehttploader-with-service-url-in-ngx-translate%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please format your question
– Jota.Toledo
Jun 21 '17 at 9:07
It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please edit your question to give a more complete description of what you expected to happen and how that differs from the actual results. See How to Ask for hints on what makes a good explanation.
– Toby Speight
Jun 21 '17 at 10:39