1 import { applyTranslations } from "./apply-translations"
2 import {
3 ItemTaxLineDTO,
4 MedusaContainer,
5 ShippingTaxLineDTO,
6 } from "@medusajs/types"
7
8 /**
9 * Applies translations to tax lines. If you are using a tax provider that doesn't have TaxRates defined in the database,
10 * you should apply the translations inside of your tax provider's `getTaxLines` method, using the `locale` provided in the context.
11 *
12 * @param taxLines - The tax lines to apply translations to.
13 * @param locale - The locale to apply translations to.
14 * @param container - The container to use for the translations.
15 * @returns The tax lines with translations applied.
16 */
17 export const applyTranslationsToTaxLines = async (
18 taxLines: ItemTaxLineDTO[] | ShippingTaxLineDTO[],
19 locale: string | undefined,
20 container: MedusaContainer
21 ) => {
22 const translatedTaxRates = taxLines.map(
23 (taxLine: ItemTaxLineDTO | ShippingTaxLineDTO) => ({
24 id: taxLine.rate_id,
25 name: taxLine.name,
26 })
27 )
28
29 await applyTranslations({
30 localeCode: locale,
31 objects: translatedTaxRates,
32 container,
33 })
34
35 const rateTranslationMap = new Map<string, string>()
36 for (const translatedRate of translatedTaxRates) {
37 if (!!translatedRate.id) {
38 rateTranslationMap.set(translatedRate.id, translatedRate.name)
39 }
40 }
41
42 for (const taxLine of taxLines) {
43 if (taxLine.rate_id) {
44 taxLine.name = rateTranslationMap.get(taxLine.rate_id)!
45 }
46 }
47
48 return taxLines
49 }