如何在Angular 12中使用NGX-Translate和I8N为Angular应用创建自定义管道

发布于 2025-02-13 07:34:58 字数 3813 浏览 1 评论 0原文

我正在Angular项目中进行国际化,就像使用 @ngx-translate一样管理门户,但我失败了。我想重做整个国际化任务的任何建议

auth.component.html

在这里我会遇到错误 - 错误NG8004:找不到名称为“翻译”的管道。

<div class="container">
  <div class="forms-container">
    <div class="signin-signup">
      <form [formGroup]="LoginFrom" (ngSubmit)="Login()" class="sign-in-form">
        <label>Change Language </label>
        <select #selLang (change)="translateLanguageTo(selLang.value)">
          <option *ngFor="let language of translate.getLangs()" [value]="language">{{ language }}</option>
        </select>
        <h2 class="title">{{'login' | translate}} </h2>
        <h4>{{'login_text' | translate}}</h4>
        <div class="input-field">
          <i class="fa fa-user"></i>
          <input (ngModelChange)="start_validaiton_Login_Form('Username')" formControlName="Username" type="text"
            placeholder="Username"/>
            <mat-icon *ngIf="!Login_Username!.invalid && started_Login_Username" class="textbox-icon">check_circle</mat-icon>
            <mat-icon *ngIf="Login_Username!.invalid && started_Login_Username" class="textbox-icon error">error</mat-icon>
        </div>
        <div class="input-field">
          <i class="fa fa-lock"></i>
          <input (ngModelChange)="start_validaiton_Login_Form('Password')" formControlName="Password" type="password"
            placeholder="Password" />
            <mat-icon *ngIf="!Login_Password!.invalid && started_Login_Password" class="textbox-icon">check_circle</mat-icon>
            <mat-icon *ngIf="Login_Password!.invalid && started_Login_Password" class="textbox-icon error">error</mat-icon>
        </div>
        <input [disabled]="!LoginFrom.valid" type="submit" value="Login" class="btn solid" />
      </form>
  </div>

auth.component.ts

import { TranslateService } from '@ngx-translate/core';
import { TranslatePipe } from '@app/pipes/translate.pipe';

@Component({
  selector: 'app-auth',
  templateUrl: './auth.component.html',
  styleUrls: ['./auth.component.scss']
})
export class AuthComponent implements OnInit {

  constructor(private router: Router, private toastr: ToastrService, private _AuthService: AuthService, 
    public translate: TranslateService
    ){
      // Register translation languages
      translate.addLangs(['en', 'er', 'fr']);
      // Set default language
      translate.setDefaultLang('en');

    } 
    //Switch language
    translateLanguageTo(lang: string) {
      this.translate.use(lang);
    }

app.module.ts

import { TranslateService } from '@ngx-translate/core';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import {TranslatePipe} from '@app/pipes/translate.pipe'
// Factory function required during AOT compilation
export function httpTranslateLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http);
}

@NgModule({
  declarations: [
    AppComponent,
    UserPermissionsComponent,
    TranslatePipe
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: httpTranslateLoaderFactory,
        deps: [HttpClient]
      }
    }),

我也尝试通过从Stakoverflow获得帮助来创建翻译管道,但错误仍然与我对Angular的新手相同对自定义管道不知道

I'm doing internationalization in my angular project it's like admin portal using @ngx-translate but I'm failing. I want to redo the whole internationalization task any suggestions how to do it

auth.component.html

here I'm getting error -- error NG8004: No pipe found with name 'translate'.

<div class="container">
  <div class="forms-container">
    <div class="signin-signup">
      <form [formGroup]="LoginFrom" (ngSubmit)="Login()" class="sign-in-form">
        <label>Change Language </label>
        <select #selLang (change)="translateLanguageTo(selLang.value)">
          <option *ngFor="let language of translate.getLangs()" [value]="language">{{ language }}</option>
        </select>
        <h2 class="title">{{'login' | translate}} </h2>
        <h4>{{'login_text' | translate}}</h4>
        <div class="input-field">
          <i class="fa fa-user"></i>
          <input (ngModelChange)="start_validaiton_Login_Form('Username')" formControlName="Username" type="text"
            placeholder="Username"/>
            <mat-icon *ngIf="!Login_Username!.invalid && started_Login_Username" class="textbox-icon">check_circle</mat-icon>
            <mat-icon *ngIf="Login_Username!.invalid && started_Login_Username" class="textbox-icon error">error</mat-icon>
        </div>
        <div class="input-field">
          <i class="fa fa-lock"></i>
          <input (ngModelChange)="start_validaiton_Login_Form('Password')" formControlName="Password" type="password"
            placeholder="Password" />
            <mat-icon *ngIf="!Login_Password!.invalid && started_Login_Password" class="textbox-icon">check_circle</mat-icon>
            <mat-icon *ngIf="Login_Password!.invalid && started_Login_Password" class="textbox-icon error">error</mat-icon>
        </div>
        <input [disabled]="!LoginFrom.valid" type="submit" value="Login" class="btn solid" />
      </form>
  </div>

auth.component.ts

import { TranslateService } from '@ngx-translate/core';
import { TranslatePipe } from '@app/pipes/translate.pipe';

@Component({
  selector: 'app-auth',
  templateUrl: './auth.component.html',
  styleUrls: ['./auth.component.scss']
})
export class AuthComponent implements OnInit {

  constructor(private router: Router, private toastr: ToastrService, private _AuthService: AuthService, 
    public translate: TranslateService
    ){
      // Register translation languages
      translate.addLangs(['en', 'er', 'fr']);
      // Set default language
      translate.setDefaultLang('en');

    } 
    //Switch language
    translateLanguageTo(lang: string) {
      this.translate.use(lang);
    }

app.module.ts

import { TranslateService } from '@ngx-translate/core';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import {TranslatePipe} from '@app/pipes/translate.pipe'
// Factory function required during AOT compilation
export function httpTranslateLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http);
}

@NgModule({
  declarations: [
    AppComponent,
    UserPermissionsComponent,
    TranslatePipe
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: httpTranslateLoaderFactory,
        deps: [HttpClient]
      }
    }),

I also try to create translate pipe by getting help from stakoverflow but error is still same as I'm new to angular I have no idea about custom pipes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇柔作态 2025-02-20 07:34:58

您遇到的错误是因为Angular无法识别管道。情况可能有很多原因,首先,您不需要translatepipe声明中,还要确保加载translatemodule(在您的imports中)在正确的模块中,即声明authcomponent的模块(在neclarations字段中)。

The error you are getting is because angular doesn't recognize the pipe. There could be many reasons why that is the case, firstly you don't need the TranslatePipe in your declarations, also make sure you load the TranslateModule (in your imports) in the correct module, that is, the module where AuthComponent is declared (in declarations field).

水染的天色ゝ 2025-02-20 07:34:58

在NGX-Translate中,您不必创建自定义管道。
您应该在component.ts中重命名翻译服务,因为它与翻译管道的名称相同,并且是公开的,因此HTML在翻译管道和公共翻译服务之间变得混淆。

In ngx-translate you dont have to create a custom pipe.
You should rename the translate service in your component.ts since it is the same name as translate pipe and it is public, the html is getting confused between the translate pipe and the public translate service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文