Skip to content

Commit b1d08ca

Browse files
authored
Merge pull request #16 from lokalise/feature/MRP-700_locales
MRP-700 Ditto locales
2 parents afcde58 + 792398e commit b1d08ca

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/modules/publish/PublishService.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ export class PublishService {
3636
}
3737

3838
const locales = Object.keys(items[0].translations)
39+
const engineLocalesMap = new Map<string, string>()
40+
const connectorLocalesMap = new Map<string, string>()
41+
locales.forEach((locale) => {
42+
const lowerCased = locale !== defaultLocale ? locale.toLowerCase() : locale
43+
engineLocalesMap.set(locale, lowerCased)
44+
connectorLocalesMap.set(lowerCased, locale)
45+
})
3946
const toUpdate: VariantUpdateData = {}
4047

41-
for (const locale of locales) {
42-
toUpdate[locale] = {}
48+
for (const engineLocale of locales) {
49+
const connectorLocale = engineLocalesMap.get(engineLocale) ?? engineLocale
50+
toUpdate[connectorLocale] = {}
4351
for (const item of items) {
44-
toUpdate[locale][item.uniqueId] = { text: item.translations[locale] }
52+
toUpdate[connectorLocale][item.uniqueId] = { text: item.translations[engineLocale] }
4553
}
4654
}
4755
const toUpdateDataEntries = Object.entries(toUpdate)
@@ -58,16 +66,17 @@ export class PublishService {
5866
if (isRejected(result) && variant) {
5967
const uniqueIdsWithError = Object.keys(data)
6068

69+
const engineVariant = connectorLocalesMap.get(variant) ?? variant
6170
uniqueIdsWithError.forEach((uniqueId) => {
6271
const existingError = publishErrors.find((error) => error.uniqueId === uniqueId)
6372
if (existingError) {
6473
existingError.perLocaleErrors = {
6574
...existingError.perLocaleErrors,
66-
...buildPerLocaleErrors(result.reason as InternalError, variant),
75+
...buildPerLocaleErrors(result.reason as InternalError, engineVariant),
6776
}
6877
} else {
6978
publishErrors.push(
70-
buildTranslatePublishError(result.reason as InternalError, uniqueId, variant),
79+
buildTranslatePublishError(result.reason as InternalError, uniqueId, engineVariant),
7180
)
7281
}
7382
})

src/modules/translate/TranslateService.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ export class TranslateService {
1414
async getContent(
1515
config: IntegrationConfig,
1616
auth: AuthConfig,
17-
locales: string[],
1817
ids: ItemIdentifiers[],
1918
// Default locale might not be needed for integration logic
2019
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2120
defaultLocale: string,
21+
originalLocalesMap: Map<string, string>,
2222
): Promise<[ContentItem[], ItemIdentifiers[]]> {
2323
const { apiKey } = auth
2424

@@ -29,6 +29,7 @@ export class TranslateService {
2929
throw new AuthFailedError()
3030
}
3131

32+
const locales = [...originalLocalesMap.keys()]
3233
const componentsByName = await this.dittoApiClient.getWorkspaceComponents(apiKey)
3334

3435
const desiredIds = ids.map((id) => id.uniqueId)
@@ -38,10 +39,11 @@ export class TranslateService {
3839

3940
const items = filteredWorkspaceComponentEntries.map(([id, data]) => {
4041
const localTexts = locales.reduce((acc, local) => {
42+
const finalLocale = originalLocalesMap.get(local) ?? local
4143
if (data.variants?.[local]) {
42-
acc[local] = data.variants[local].text
44+
acc[finalLocale] = data.variants[local].text
4345
} else {
44-
acc[local] = ''
46+
acc[finalLocale] = ''
4547
}
4648

4749
return acc

src/modules/translate/translateController.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ export const getContent = async (
88
) => {
99
const { translateService } = req.diScope.cradle
1010

11+
const originalLocalesMap = new Map<string, string>()
12+
req.body.locales.forEach((locale) => {
13+
const lowerCased = locale !== req.body.defaultLocale ? locale.toLowerCase() : locale
14+
originalLocalesMap.set(lowerCased, locale)
15+
})
16+
1117
const [items, updateItems] = await translateService.getContent(
1218
req.integrationConfig,
1319
req.authConfig,
14-
req.body.locales,
1520
req.body.items,
1621
req.body.defaultLocale,
22+
originalLocalesMap,
1723
)
1824

1925
await reply.send({ items, updateItems })

0 commit comments

Comments
 (0)