找不到 ngx-translate JSON
ngx-translate JSON not found
我意识到这是一个常见问题,并且一直在搜索此存储库的支持票证和 Stack Overflow 以寻找我无法正常工作的原因。 None 我尝试过的许多建议都奏效了。我很困惑,因为我的设置很简单而且(我可以看到)与 ngx-translate 存储库中共享的示例代码设置没有任何不同。
我正在使用 Angular 5.1.0 和 Angular CLI 1.7。 webpack和ngx-translate版本如下:
"@ngx-translate/core": "^9.1.1",
"@ngx-translate/http-loader": "^2.0.1",
"webpack": "~3.8.1"
http://localhost:4200/src/assets/i18n/en.json 在浏览器中正确解析为文件,因此文件位于 ngx-translate README 中概述的默认配置的正确位置。
相关文件如下:
app.component.ts
import { ActivatedRoute } from '@angular/router';
import { Component, ElementRef, HostListener, OnInit, ViewChild, AfterViewInit, ErrorHandler } from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
/*
* Main app component that houses all views.
*/
@Component({
selector: 'app-comp',
templateUrl: './app.component.html'
})
export class AppComponent {
store: any;
constructor(private route: ActivatedRoute, private translate: TranslateService) {
// translate.addLangs(['en', 'fr']);
translate.setDefaultLang('en');
}
}
app.module.ts
import { MaterialModule } from './material.module';
import { BrowserModule, Title } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpModule } from '@angular/http';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { RootComponent } from './root.component';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.routing';
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from '../db/in-memory-data.service';
import { AppInterceptor } from './_core/app.interceptor';
import { FooterComponent } from './_core/footer/footer.component';
import { HeaderComponent } from './_core/header/header.component';
import { DataService } from './_core/data.service';
import { DataResolver } from './_core/data.resolver';
import { environment } from '../environments/environment';
import { AppGuard } from './app.guard';
import { MessageService } from './message.service';
import { GatewayService } from './shared/gateway.service';
import { WindowRefService } from './window-ref.service';
/*
* Main module for the app that boostraps everything.
*/
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
}),
HttpModule,
FormsModule,
AppRoutingModule,
BrowserAnimationsModule,
InMemoryWebApiModule.forRoot(InMemoryDataService, { dataEncapsulation: false }) // Remove to use real HTTP calls
],
declarations: [
RootComponent,
HeaderComponent,
AppComponent,
FooterComponent
],
providers: [
AppGuard,
DataService,
DataResolver,
MessageService,
GatewayService,
WindowRefService,
{
provide: HTTP_INTERCEPTORS,
useClass: AppInterceptor,
multi: true,
}
],
bootstrap: [RootComponent]
})
export class AppModule { }
// required for AOT compilation
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
我尝试了所有常见的解决方案,包括使用 CopyWebpackPlugin 尝试解决 webpack 捆绑,将 i18n 目录添加到 angular-cli.json 中的资产列表,并显式传递我的 TranslateHttpLoader 路径是这样的:
return new TranslateHttpLoader(http, "./assets/i18n/", ".json");
None 这些方法奏效了。
我的设置中是否缺少一些非常基本的东西?我觉得在这一点上我已经看了太久了。 :/
按如下方式更改您的 JSON 路径:
return new TranslateHttpLoader(http, "assets/i18n/", ".json");
因为./
选择当前目录。
如果其他人遇到此问题,修复方法是删除 AppInterceptor,正如@hrdkisback 在上面的评论中所建议的那样。我以为我已经删除了它的所有实例,但我没有。一旦完成,我就可以访问翻译文件了。
尝试将资源文件 en.json 重命名为 en.txt 并更改 app.module.ts:
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/resources/', '.txt');
}
我意识到这是一个常见问题,并且一直在搜索此存储库的支持票证和 Stack Overflow 以寻找我无法正常工作的原因。 None 我尝试过的许多建议都奏效了。我很困惑,因为我的设置很简单而且(我可以看到)与 ngx-translate 存储库中共享的示例代码设置没有任何不同。
我正在使用 Angular 5.1.0 和 Angular CLI 1.7。 webpack和ngx-translate版本如下:
"@ngx-translate/core": "^9.1.1",
"@ngx-translate/http-loader": "^2.0.1",
"webpack": "~3.8.1"
http://localhost:4200/src/assets/i18n/en.json 在浏览器中正确解析为文件,因此文件位于 ngx-translate README 中概述的默认配置的正确位置。
相关文件如下:
app.component.ts
import { ActivatedRoute } from '@angular/router';
import { Component, ElementRef, HostListener, OnInit, ViewChild, AfterViewInit, ErrorHandler } from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
/*
* Main app component that houses all views.
*/
@Component({
selector: 'app-comp',
templateUrl: './app.component.html'
})
export class AppComponent {
store: any;
constructor(private route: ActivatedRoute, private translate: TranslateService) {
// translate.addLangs(['en', 'fr']);
translate.setDefaultLang('en');
}
}
app.module.ts
import { MaterialModule } from './material.module';
import { BrowserModule, Title } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClient, HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpModule } from '@angular/http';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { RootComponent } from './root.component';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.routing';
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from '../db/in-memory-data.service';
import { AppInterceptor } from './_core/app.interceptor';
import { FooterComponent } from './_core/footer/footer.component';
import { HeaderComponent } from './_core/header/header.component';
import { DataService } from './_core/data.service';
import { DataResolver } from './_core/data.resolver';
import { environment } from '../environments/environment';
import { AppGuard } from './app.guard';
import { MessageService } from './message.service';
import { GatewayService } from './shared/gateway.service';
import { WindowRefService } from './window-ref.service';
/*
* Main module for the app that boostraps everything.
*/
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
}),
HttpModule,
FormsModule,
AppRoutingModule,
BrowserAnimationsModule,
InMemoryWebApiModule.forRoot(InMemoryDataService, { dataEncapsulation: false }) // Remove to use real HTTP calls
],
declarations: [
RootComponent,
HeaderComponent,
AppComponent,
FooterComponent
],
providers: [
AppGuard,
DataService,
DataResolver,
MessageService,
GatewayService,
WindowRefService,
{
provide: HTTP_INTERCEPTORS,
useClass: AppInterceptor,
multi: true,
}
],
bootstrap: [RootComponent]
})
export class AppModule { }
// required for AOT compilation
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
我尝试了所有常见的解决方案,包括使用 CopyWebpackPlugin 尝试解决 webpack 捆绑,将 i18n 目录添加到 angular-cli.json 中的资产列表,并显式传递我的 TranslateHttpLoader 路径是这样的:
return new TranslateHttpLoader(http, "./assets/i18n/", ".json");
None 这些方法奏效了。
我的设置中是否缺少一些非常基本的东西?我觉得在这一点上我已经看了太久了。 :/
按如下方式更改您的 JSON 路径:
return new TranslateHttpLoader(http, "assets/i18n/", ".json");
因为./
选择当前目录。
如果其他人遇到此问题,修复方法是删除 AppInterceptor,正如@hrdkisback 在上面的评论中所建议的那样。我以为我已经删除了它的所有实例,但我没有。一旦完成,我就可以访问翻译文件了。
尝试将资源文件 en.json 重命名为 en.txt 并更改 app.module.ts:
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/resources/', '.txt');
}