[英]JsonReaderException: Unexpected character encountered while parsing value ngx-translate
I am making a .NET Core application with Angular. 我正在用Angular制作.NET Core应用程序。
I am having problems with making ngx-translate work for a while now. 我暂时无法使ngx-translate工作。
I have managed to make it partially work. 我设法使其部分工作。
With the answer to my previous problems from: 对于我以前的问题,答案来自:
https://github.com/aspnet/Templates/issues/866#issuecomment-326737781 https://github.com/aspnet/Templates/issues/866#issuecomment-326737781
and tutorial from: 和教程来自:
https://medium.com/letsboot/translate-angular-4-apps-with-ngx-translate-83302fb6c10d https://medium.com/letsboot/translate-angular-4-apps-with-ngx-translate-83302fb6c10d
I get Internal Server Error: 我收到内部服务器错误:
JsonReaderException: Unexpected character encountered while parsing value: {. Path 'errorMessage', line 1, position 17.
Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)
Newtonsoft.Json.JsonTextReader.ReadAsString()
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, bool hasConverter)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, string id)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue)
when I try to run the application 当我尝试运行应用程序时
I did no modifications to the project. 我没有对该项目进行任何修改。
In app.component I have: 在app.component我有:
import { Component } from '@angular/core';
import { TranslateService } from "@ngx-translate/core";
@Component({
selector: 'app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private translate: TranslateService) {
// this language will be used as a fallback when a translation isn't found in the current language
translate.setDefaultLang('en');
// the lang to use, if the lang isn't available, it will use the current loader to get them
translate.use('en');
}
switchLanguage(language: string) {
this.translate.use(language);
}
}
If I comment the lines: 如果我注释以下行:
translate.setDefaultLang('en');
...
translate.use('en');
The application starts. 应用程序启动。 Then if I uncomment the first line, the project automatically rebuilds and it actually goes in home component and replaces:
然后,如果我取消第一行的注释,则项目将自动重建,并且它实际上进入了home组件并替换为:
{{ 'Intro' | translate }}
"Intro" with the value in the en.json file...so it kind of works. 带有en.json文件中的值的“简介” ...因此可以正常工作。 The problem only appears when I run the project
该问题仅在我运行项目时出现
My en.json file: 我的en.json文件:
{
"HELLO": "hello",
"Intro": "Hello I am Arthur, I am 42 years old.",
"Title": "Translation example"
}
My other json file: 我的其他json文件:
{
"HELLO": "salut",
"Intro": "Bonjour je m'appelle Arthur, j'ai 42 ans.",
"Title": "Exemple de traduction"
}
They are both in assets/i18n in wwwroot. 它们都在wwwroot中的资产/ i18n中。
I do not call Newtonsoft.Json.JsonTextReader so, I guess ngx-translate does it in the background. 我不调用Newtonsoft.Json.JsonTextReader,所以我猜ngx-translate在后台执行了它。
What am I doing wrong? 我究竟做错了什么?
The project can be found here: 该项目可以在这里找到:
https://github.com/dobrinsky/AngularDefault https://github.com/dobrinsky/AngularDefault
I found a solution. 我找到了解决方案。 I
一世
From https://github.com/MarkPieszak/aspnetcore-angular2-universal 从https://github.com/MarkPieszak/aspnetcore-angular2-universal
I knew that we should load the files like this: 我知道我们应该像这样加载文件:
export function createTranslateLoader(http: Http, baseHref) {
// Temporary Azure hack
if (baseHref === null && typeof window !== 'undefined') {
baseHref = window.location.origin;
}
// i18n files are in `wwwroot/assets/`
return new TranslateHttpLoader(http, `${baseHref}/assets/i18n/`, '.json');
}
but is not enough. 但是还不够。 We also need to change the TranslateModule.forRoot like this:
我们还需要像这样更改TranslateModule.forRoot:
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient, [ORIGIN_URL]]
}
})
where 哪里
ORIGIN_URL is in another file: ORIGIN_URL在另一个文件中:
import { ORIGIN_URL } from "./constansts/baseurl.constants";
like this: 像这样:
import { InjectionToken } from '@angular/core';
export const ORIGIN_URL = new InjectionToken<string>('ORIGIN_URL');
we need to provide ORIGIN_URL in boot.server.ts like this: 我们需要在boot.server.ts中提供ORIGIN_URL,如下所示:
const providers = [
{ provide: INITIAL_CONFIG, useValue: { document: '<app></app>', url: params.url } },
{ provide: APP_BASE_HREF, useValue: params.baseUrl },
{ provide: 'BASE_URL', useValue: params.origin + params.baseUrl },
{ provide: ORIGIN_URL, useValue: params.origin }
];
Also, in app.module.browser.ts:
export function getOriginUrl() {
return window.location.origin;
}
@NgModule({
bootstrap: [ AppComponent ],
imports: [
BrowserModule,
AppModuleShared
],
providers: [
{
// We need this for our Http calls since they'll be using an ORIGIN_URL provided in main.server
// (Also remember the Server requires Absolute URLs)
provide: ORIGIN_URL,
useFactory: (getOriginUrl)
},
{
provide:
'BASE_URL', useFactory: getBaseUrl
}
]
})
export class AppModule {
}
And like this it works for me... 像这样,它对我有用...
Again, my project can be found here: https://github.com/dobrinsky/AngularDefault 同样,可以在这里找到我的项目: https : //github.com/dobrinsky/AngularDefault
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.