简体   繁体   English

Angular i18n 和本地化:如何根据语言环境检索货币符号?

[英]Angular i18n and localize: How to retrieve currency symbol according to locale?

I am using Angular 13 and I would like to figure out how to retrieve currency symbol according to locales.我正在使用 Angular 13,我想弄清楚如何根据语言环境检索货币符号 I am using Angular's localize package and i18n to translate text and data.我正在使用 Angular 的localize 包i18n来翻译文本和数据。 Everything works fine in both versions (English and Italian - translations files are not linked) except that when running server with configuration set to my italian version of the app, instead of displaying € it displays USD.在两个版本中一切正常(英语和意大利语 - 翻译文件未链接),除了在运行服务器并将配置设置为我的应用程序的意大利语版本时,它不显示欧元,而是显示美元。 My goal would be to display € when app is running in italian.我的目标是在应用程序以意大利语运行时显示€。

On Angular's docs I have found out a function called getCurrencySymbol but I wasn't able to figure out how to implement it properly.在 Angular 的文档中,我发现了一个名为getCurrencySymbol的函数,但我无法弄清楚如何正确实现它。

It may seem easy but as a junior, I could not find a proper solution.这似乎很容易,但作为一名大三学生,我找不到合适的解决方案。

Here some of my settings:这是我的一些设置:

angular.json角.json

 { "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "cli": { "analytics": false }, "version": 1, "newProjectRoot": "projects", "projects": { "i18ndemo": { "projectType": "application", "i18n": { "locales": { "it": "src/locale/messages.it.xlf" } }, "schematics": { "@schematics/angular:application": { "strict": true } }, "root": "", "sourceRoot": "src", "prefix": "app", "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": { "outputPath": "dist/i18ndemo", "index": "src/index.html", "main": "src/main.ts", "polyfills": "src/polyfills.ts", "tsConfig": "tsconfig.app.json", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "node_modules/bootstrap/dist/css/bootstrap.min.css", "src/styles.css" ], "scripts": [ "node_modules/bootstrap/dist/js/bootstrap.min.js" ] }, "configurations": { "production": { "budgets": [ { "type": "initial", "maximumWarning": "500kb", "maximumError": "1mb" }, { "type": "anyComponentStyle", "maximumWarning": "2kb", "maximumError": "4kb" } ], "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.prod.ts" } ], "outputHashing": "all" }, "development": { "buildOptimizer": false, "optimization": false, "vendorChunk": true, "extractLicenses": false, "sourceMap": true, "namedChunks": true }, "it": { "localize": [ "it" ] } }, "defaultConfiguration": "production" }, "serve": { "builder": "@angular-devkit/build-angular:dev-server", "configurations": { "production": { "browserTarget": "i18ndemo:build:production" }, "development": { "browserTarget": "i18ndemo:build:development" }, "it": { "browserTarget": "i18ndemo:build:it" } }, "defaultConfiguration": "development" }, "extract-i18n": { "builder": "@angular-devkit/build-angular:extract-i18n", "options": { "browserTarget": "i18ndemo:build" } }, "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "main": "src/test.ts", "polyfills": "src/polyfills.ts", "tsConfig": "tsconfig.spec.json", "karmaConfig": "karma.conf.js", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "src/styles.css" ], "scripts": [] } } } } }, "defaultProject": "i18ndemo" }

html html

 <div class="card"> <h1 i18n="@@title" class="text-center">Your order is on it's way!</h1> <div class="d-flex align-items-center"> <img src="../../assets/delivery.png" alt="Delivery truck clipart" i18n-alt="@@image-alt"> <table class="table"> <thead> <tr> <th scope="col" i18n="@@items">Items</th> <th scope="col" i18n="@@quantity">Qty</th> <th scope="col" i18n="@@amount">Amount</th> </tr> </thead> <tbody> <tr *ngFor="let item of items"> <td>{{ item.name }}</td> <td> {{ item.quantity }}</td> <td>{{ item.price | currency }}</td> </tr> <tr> <td> {{ currentDate | date }} </td> <td i18n="@@total">Total </td> <td>{{ 75.60 | currency }}</td> </tr> </tbody> </table> </div> <p class="text-center" i18n="@@subtitle">Your order has been processed. Thank you for shopping with us &hearts;</p> </div>

 import { Component } from '@angular/core'; export interface Item { name: string, quantity: number, price: number } @Component({ selector: 'app-card', templateUrl: './card.component.html', styleUrls: ['./card.component.css'] }) export class CardComponent { currentDate = new Date(); items: Item[] = [ { name: $localize`:@@sunscreen:High protection sunscreen SPF50+`, quantity: 2, price: 35.90 }, { name: $localize`:@@nail-polish:Nail polish`, quantity: 3, price: 28.50 }, { name: $localize`:@@perfume:Lilac and lavander natural perfume`, quantity: 1, price: 13.30 }, { name: $localize`:@@shampoo:Shampoo for sensitive scalp`, quantity: 1, price: 15.90 }, ] }

Found a handy solution.找到了一个方便的解决方案。 I know my case is peculiar because in a real use case, data would be retrieved from different databases for different locales, but since I was developing a demo project to test i18n on Angular, I needed two different currencies for the same value.我知道我的案例很特殊,因为在实际用例中,数据将从不同的数据库中针对不同的语言环境进行检索,但是由于我正在开发一个演示项目来在 Angular 上测试 i18n,因此我需要两种不同的货币来获得相同的价值。 Anyway, this is what has been done:无论如何,这就是已经完成的:

 providers: [{ provide: DEFAULT_CURRENCY_CODE, useFactory: (locale: string) => locale === 'it' ? 'EUR' : 'USD', deps: [LOCALE_ID] }],

and in my html I am using Angular's currency pipe.在我的 html 中,我使用的是 Angular 的货币管道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM