首页 文章

找不到ngx-translate JSON

提问于
浏览
0

我意识到这是一个常见的问题,并且一直在搜索这个repo的支持票和Stack Overflow,寻找为什么我无法使用它 . 我尝试过的许多建议都没有奏效 . 我很困惑因为我的设置很简单,而且(我可以看到)与ngx-translate repo中共享的示例代码设置不同 .

我正在使用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");

这些方法都没有奏效 .

我的设置中是否有一些非常基本的东西?我觉得我此刻已经看了太长时间 . :/

3 回答

  • 0

    如果其他人遇到此问题,则修复程序正在删除AppInterceptor,正如@hrdkisback在上面的评论中所建议的那样 . 我以为我已经删除了它的所有实例,但我没有 . 完成后,我就可以访问翻译文件了 .

  • 1

    更改您的JSON路径,如下所示:

    return new TranslateHttpLoader(http, "assets/i18n/", ".json");
    

    由于 ./ 选择当前目录 .

  • 0

    尝试将资源文件en.json重命名为en.txt并更改app.module.ts:

    export function HttpLoaderFactory(http: HttpClient) { return new TranslateHttpLoader(http, './assets/resources/', '.txt'); }

相关问题