这里是一个最小的复制:link

我正在使用Angular(17)与Transloco. 在遵循文档中的说明之后,我try 使用模板中的 struct 指令翻译简单的文本,

<ng-container *transloco="let t"> <h2>{{ t('greeting') }}</h2> </ng-container>

问题是,正如您所看到的,根本没有呈现文本.

我试着使用相对和绝对路径的翻译文件,也注意到网络选项卡中没有HTTP调用,这表明翻译没有被提取.

完整代码

双手

import { Component, inject, isDevMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';
import { provideHttpClient } from '@angular/common/http';
import { TranslocoHttpLoader } from './transloco-loader';
import { TranslocoService, provideTransloco } from '@jsverse/transloco';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <ng-container *transloco="let t">
      <h2>{{ t('greeting') }}</h2>
    </ng-container>
    <button (click)="onChangeToSpanish()">Change to Spanish</button>
  `,
})
export class App {
  private translocoService = inject(TranslocoService);

  onChangeToSpanish() {
    this.translocoService.setActiveLang('es');
  }
}

bootstrapApplication(App, {
  providers: [
    provideHttpClient(),
    provideTransloco({
      config: {
        availableLangs: ['en', 'es'],
        defaultLang: 'en',
        // Remove this option if your application doesn't support changing language in runtime.
        reRenderOnLangChange: true,
        prodMode: !isDevMode(),
      },
      loader: TranslocoHttpLoader,
    }),
  ],
});

transloco-loader.ts

import { inject, Injectable } from '@angular/core';
import { Translation, TranslocoLoader } from '@jsverse/transloco';
import { HttpClient } from '@angular/common/http';

@Injectable({ providedIn: 'root' })
export class TranslocoHttpLoader implements TranslocoLoader {
  private http = inject(HttpClient);

  getTranslation(lang: string) {
    return this.http.get<Translation>(`/assets/i18n/${lang}.json`);
  }
}

因为这应该是直接通过,我觉得我错过了什么.

推荐答案

代码的问题如下.

  1. 因为我们使用的是*transloco struct 指令,所以需要将import TranslocoDirective添加到独立组件的imports数组中!

  2. Anular.json文件中,我们需要引用assets文件夹,以便翻译文件在我们查找时被解析

Anular.json

...
"options": {
    "assets": ["src/assets"],
    ...

完整代码

import { Component, inject, isDevMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';
import { provideHttpClient } from '@angular/common/http';
import { TranslocoHttpLoader } from './transloco-loader';
import {
  TranslocoService,
  provideTransloco,
  TranslocoDirective,
} from '@jsverse/transloco';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [TranslocoDirective],
  template: `
    <ng-container *transloco="let t">
      <h2>{{ t('greeting') }}</h2>
    </ng-container>
    <button (click)="onChangeToSpanish()">Change to Spanish</button>
  `,
})
export class App {
  private translocoService = inject(TranslocoService);

  onChangeToSpanish() {
    this.translocoService.setActiveLang('es');
  }
}

bootstrapApplication(App, {
  providers: [
    provideHttpClient(),
    provideTransloco({
      config: {
        availableLangs: ['en', 'es'],
        defaultLang: 'en',
        // Remove this option if your application doesn't support changing language in runtime.
        reRenderOnLangChange: true,
        prodMode: !isDevMode(),
      },
      loader: TranslocoHttpLoader,
    }),
  ],
});

Stackblitz Demo

Angular相关问答推荐

根据Angular中的构建来卸载独立组件

接收错误NullInjectorError:R3InjectorError(_AppModule)[config—config]....>过度安装Angular—Markdown—Editor

对子router-outlet 应用主机样式

CDK虚拟滚动由于组件具有注入属性而在滚动时看到错误的值

Angular 17多内容投影错误

从API动态加载MAT表数据时禁用分页

无法在单个元素上多次监听同一个事件

在 kubernetes 上创建 Ingress 服务以将 springboot [后端] 连接到前端 [angular]

导入 AngularFirestoreModule 时出现问题:此类型参数可能需要 `extends firebase.firestore.DocumentData` 约束.

Angular:将间隔与增量和减量相结合

在 angular2 中,如何获取在为 @Input 发送的对象上更改的属性的 onChanges

Angular 2在单击子项时防止单击父项

找不到模块'@angular/compiler'

如何在 Angular CLI 1.1.1 版中将路由模块添加到现有模块?

在 Angular 4 中播放声音

Angular2 中的providers提供者是什么意思?

Angular2 Base64 清理不安全的 URL 值

如何提高 Angular2 应用程序的加载性能?

实现autocomplete功能

我应该在 Angular 4+ 应用程序中 for each 组件创建一个模块吗?