如何翻译本地JSON数据

iqxoj9l9  于 2023-11-20  发布在  其他
关注(0)|答案(2)|浏览(144)

navbar.component.html

<div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav ml-auto">
            <li routerLinkActive="active" *ngFor="let menuItem of menuItems" class="{{menuItem.class}} nav-item">
                <a [routerLink]="[menuItem.path]" class="nav-link">{{menuItem.title}}</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="" data-toggle="modal" data-target="#myModal" (click)="showModal()"><i class="icon-search"></i></a>
            </li>
        </ul>
    </div>

字符串
navbar.comomponent.ts

import { Component, OnInit } from '@angular/core';

import {TranslateService} from '@ngx-translate/core';

declare interface RouteInfo {
    path: string;
    title: string;
    class: string;
}
export const ROUTES: RouteInfo[] = [
    { path: '/home', title: 'Home', class: '' },
    { path: '/buyer_registration', title: 'Buyer Registration',  class: '' },
    { path: '/rate_contract', title: 'Rate Contract', class: '' },
    { path: '/login', title: 'Login', class: '' },
];

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
})

export class NavbarComponent implements OnInit {
  menuItems: any[];

  constructor(translate: TranslateService) {
    translate.setDefaultLang('English');
    translate.use('English');
  }

  ngOnInit() {
    this.menuItems = ROUTES.filter(menuItem => menuItem);
  }
}


如何翻译本地JSON数据?
我已经有一个en和fr JSON文件在我的本地

dgenwo3n

dgenwo3n1#

我也遇到过这个问题。
我最后做的是使用ngx-translate。它很容易使用!
Here is working example.

j13ufse2

j13ufse22#

有了ngx-translate,你可以做到这一点。你会发现一步一步的介绍在这里:
ngx-translate Documentation

相关问题