【发布时间】:2021-11-03 10:36:45
【问题描述】:
我正在使用 ngx-translate 来管理 Angular 12 中的国际化,并且我正在使用 API 来检索运行良好的语言文件,但我不确定如何将其他参数传递给 API 以执行其他操作逻辑来决定正确的语言文件。
这是我目前的设置: 在我的 app.module.ts
.
.
.
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
]
.
.
.
export function HttpLoaderFactory(httpClient: HttpClient) {
return new TranslateHttpLoader(
httpClient,
'http://translation-service.remote.com/',
''
);
}
你可以看到我正在调用一个远程端点 http://translation-service.remote.com/ 它将返回给我语言 json 文件
这是我的 app.component.ts
中的内容 export class AppComponent {
constructor(public translate: TranslateService){
translate.addLangs(['en-us', 'en-gb']);
translate.setDefaultLang(`en-us`);
}
}
最后来自我的 app.component.html
<select #langSelect (change)="translate.use(langSelect.value)">
<option *ngFor="let lang of translate.getLangs()" [value]="lang" [selected]="lang === translate.currentLang">{{lang }}</option>
</select>
当应用程序加载或用户选择一种语言时,ngx-translate 会发送如下所示的请求:http://translation-service.remote.com/[select-language](例如http://translation-service.remote.com/en-us)
现在,我希望能够将查询字符串参数传递给翻译 api GET 请求。例如:http://translation-service.remote.com/en-us?extraParam=abc 无需重新加载页面,但我找不到实现该目标的方法。
【问题讨论】:
标签: angular ngx-translate