The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(i18n): translate Twenty's own labels on custom and application objects (#27328)
Mahmoud Abas92a91637e902
diff --git a/packages/twenty-server/src/engine/core-modules/i18n/utils/__tests__/translate-standard-label.util.spec.ts b/packages/twenty-server/src/engine/core-modules/i18n/utils/__tests__/translate-standard-label.util.spec.tsindex 6a507da9fa7..e67a145b027 100644--- a/packages/twenty-server/src/engine/core-modules/i18n/utils/__tests__/translate-standard-label.util.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/i18n/utils/__tests__/translate-standard-label.util.spec.ts@@ -22,7 +22,6 @@ describe('translateStandardLabel', () => {it('should return the source value when it is empty', () => {const result = translateStandardLabel({sourceValue: '',- isStandardApp: true,applicationCatalog: undefined,i18nInstance: mockI18n,});@@ -36,7 +35,6 @@ describe('translateStandardLabel', () => {const result = translateStandardLabel({sourceValue: 'Company',- isStandardApp: false,applicationCatalog: { 'company-id': 'Entreprise' },i18nInstance: mockI18n,});@@ -45,12 +43,38 @@ describe('translateStandardLabel', () => {expect(mockI18n._).not.toHaveBeenCalled();});- it('should fall back to the source value when the catalog has no matching entry', () => {+ it("should fall back to Twenty's catalog when the application catalog has no matching entry", () => {+ mockGenerateMessageId.mockReturnValue('creation-date-id');+ mockI18n._.mockReturnValue('Date de création');++ const result = translateStandardLabel({+ sourceValue: 'Creation date',+ applicationCatalog: {},+ i18nInstance: mockI18n,+ });++ expect(result).toBe('Date de création');+ });++ it("should fall back to Twenty's catalog when the application catalog entry is empty", () => {+ mockGenerateMessageId.mockReturnValue('creation-date-id');+ mockI18n._.mockReturnValue('Date de création');++ const result = translateStandardLabel({+ sourceValue: 'Creation date',+ applicationCatalog: { 'creation-date-id': '' },+ i18nInstance: mockI18n,+ });++ expect(result).toBe('Date de création');+ });++ it('should return the source value when neither catalog has a translation', () => {mockGenerateMessageId.mockReturnValue('missing-id');+ mockI18n._.mockReturnValue('missing-id');const result = translateStandardLabel({sourceValue: 'Company',- isStandardApp: false,applicationCatalog: {},i18nInstance: mockI18n,});@@ -64,7 +88,6 @@ describe('translateStandardLabel', () => {const result = translateStandardLabel({sourceValue: 'Company',- isStandardApp: true,applicationCatalog: { 'company-id': 'Entreprise' },i18nInstance: mockI18n,});@@ -79,7 +102,6 @@ describe('translateStandardLabel', () => {const result = translateStandardLabel({sourceValue: 'Company',- isStandardApp: true,applicationCatalog: undefined,i18nInstance: mockI18n,});@@ -99,7 +121,6 @@ describe('translateStandardLabel', () => {const result = translateStandardLabel({sourceValue: 'Company',- isStandardApp: true,applicationCatalog: undefined,i18nInstance: mockI18n,});@@ -107,15 +128,21 @@ describe('translateStandardLabel', () => {expect(result).toBe('Company');});- it('should return the source value for a non-standard app without a catalog', () => {+ it('should translate a label Twenty wrote itself when the application has no catalog', () => {+ mockGenerateMessageId.mockReturnValue('go-to-id');+ mockI18n._.mockReturnValue('Aller à {objectLabelPlural}');+const result = translateStandardLabel({- sourceValue: 'Company',- isStandardApp: false,+ sourceValue: 'Go to {objectLabelPlural}',+ context: 'commandMenuItem.label',applicationCatalog: undefined,i18nInstance: mockI18n,});- expect(result).toBe('Company');- expect(mockGenerateMessageId).not.toHaveBeenCalled();+ expect(result).toBe('Aller à {objectLabelPlural}');+ expect(mockGenerateMessageId).toHaveBeenCalledWith(+ 'Go to {objectLabelPlural}',+ 'commandMenuItem.label',+ );});});diff --git a/packages/twenty-server/src/engine/core-modules/i18n/utils/translate-standard-label.util.ts b/packages/twenty-server/src/engine/core-modules/i18n/utils/translate-standard-label.util.tsindex 91375895749..2571df10a5a 100644--- a/packages/twenty-server/src/engine/core-modules/i18n/utils/translate-standard-label.util.ts+++ b/packages/twenty-server/src/engine/core-modules/i18n/utils/translate-standard-label.util.ts@@ -1,5 +1,4 @@import { isNonEmptyString } from '@sniptt/guards';-import { isDefined } from 'twenty-shared/utils';import {generateMessageId,@@ -11,13 +10,11 @@ import { type MessageIdTranslator } from 'src/engine/metadata-modules/overrides/export const translateStandardLabel = ({sourceValue,context,- isStandardApp,applicationCatalog,i18nInstance,}: {sourceValue: string;context?: string;- isStandardApp: boolean;applicationCatalog: Record<string, string> | undefined;i18nInstance: MessageIdTranslator;}): string => {@@ -25,24 +22,19 @@ export const translateStandardLabel = ({return sourceValue ?? '';}- if (!isDefined(applicationCatalog) && !isStandardApp) {- return sourceValue;- }-const messageId = generateMessageId(sourceValue, context);+ const catalogTranslation = applicationCatalog?.[messageId];- if (isDefined(applicationCatalog)) {- return applicationCatalog[messageId] ?? sourceValue;+ if (isNonEmptyString(catalogTranslation)) {+ return catalogTranslation;}- if (isStandardApp) {- const translatedMessage = i18nInstance._(- messageId,- METADATA_LABEL_PLACEHOLDER_PASS_THROUGH,- );-- return translatedMessage === messageId ? sourceValue : translatedMessage;- }+ const translatedMessage = i18nInstance._(+ messageId,+ METADATA_LABEL_PLACEHOLDER_PASS_THROUGH,+ );- return sourceValue;+ return isNonEmptyString(translatedMessage) && translatedMessage !== messageId+ ? translatedMessage+ : sourceValue;};diff --git a/packages/twenty-server/src/engine/metadata-modules/overrides/utils/__tests__/resolve-effective-entity-property.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/overrides/utils/__tests__/resolve-effective-entity-property.util.spec.tsindex 3ff677496ac..8f19436654b 100644--- a/packages/twenty-server/src/engine/metadata-modules/overrides/utils/__tests__/resolve-effective-entity-property.util.spec.ts+++ b/packages/twenty-server/src/engine/metadata-modules/overrides/utils/__tests__/resolve-effective-entity-property.util.spec.ts@@ -49,7 +49,16 @@ const frozenResolveObjectOverride = (}if (!isStandardApp && !isDefined(applicationCatalog)) {- return objectMetadata[labelKey] ?? '';+ return labelKey !== 'icon' &&+ labelKey !== 'color' &&+ !isDefined(objectMetadata.overrides?.[labelKey])+ ? translateStandardLabel({+ sourceValue: objectMetadata[labelKey] ?? '',+ context: `objectMetadata.${labelKey}`,+ applicationCatalog,+ i18nInstance,+ })+ : (objectMetadata[labelKey] ?? '');}if (@@ -68,7 +77,6 @@ const frozenResolveObjectOverride = (return translateStandardLabel({sourceValue: objectMetadata[labelKey] ?? '',context: `objectMetadata.${labelKey}`,- isStandardApp,applicationCatalog,i18nInstance,});@@ -94,7 +102,15 @@ const frozenResolveFieldOverride = (}if (!isStandardApp && !isDefined(applicationCatalog)) {- return fieldMetadata[labelKey] ?? '';+ return labelKey !== 'icon' &&+ !isDefined(fieldMetadata.overrides?.[labelKey])+ ? translateStandardLabel({+ sourceValue: fieldMetadata[labelKey] ?? '',+ context: `fieldMetadata.${labelKey}`,+ applicationCatalog,+ i18nInstance,+ })+ : (fieldMetadata[labelKey] ?? '');}if (labelKey === 'icon' && isDefined(fieldMetadata.overrides?.icon)) {@@ -108,7 +124,6 @@ const frozenResolveFieldOverride = (return translateStandardLabel({sourceValue: fieldMetadata[labelKey] ?? '',context: `fieldMetadata.${labelKey}`,- isStandardApp,applicationCatalog,i18nInstance,});@@ -316,7 +331,7 @@ const frozenResolveBespokeProperty = (overrides: AnyOverrides,property: string,metadataLabelContext: string,- isStandardApp: boolean,+ _isStandardApp: boolean,i18nInstance: I18n,applicationCatalog?: Record<string, string>,): string => {@@ -327,7 +342,6 @@ const frozenResolveBespokeProperty = (return translateStandardLabel({sourceValue: baseValue,context: metadataLabelContext,- isStandardApp,applicationCatalog,i18nInstance,});diff --git a/packages/twenty-server/src/engine/metadata-modules/overrides/utils/__tests__/resolve-effective-translated-flat-entity.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/overrides/utils/__tests__/resolve-effective-translated-flat-entity.util.spec.tsindex 44615878a98..e13790d222c 100644--- a/packages/twenty-server/src/engine/metadata-modules/overrides/utils/__tests__/resolve-effective-translated-flat-entity.util.spec.ts+++ b/packages/twenty-server/src/engine/metadata-modules/overrides/utils/__tests__/resolve-effective-translated-flat-entity.util.spec.ts@@ -1,5 +1,7 @@+import { setupI18n } from '@lingui/core';import { generateMessageId } from 'twenty-shared/i18n';+import { messages as frenchMessages } from 'src/engine/core-modules/i18n/locales/generated/fr-FR';import { type EffectiveEntityI18nContext } from 'src/engine/metadata-modules/overrides/types/effective-entity-i18n-context.type';import { resolveEffectiveTranslatedFlatEntity } from 'src/engine/metadata-modules/overrides/utils/resolve-effective-translated-flat-entity.util';@@ -72,19 +74,48 @@ describe('resolveEffectiveTranslatedFlatEntity', () => {expect(effective.isActive).toBe(false);});- it('leaves a custom entity without a catalog untranslated', () => {+ const resolveCustomLabel = (+ label: string,+ i18nInstance: EffectiveEntityI18nContext['i18nInstance'],+ ) =>+ resolveEffectiveTranslatedFlatEntity({+ metadataName: 'fieldMetadata',+ flatEntity: {+ ...flatFieldMetadata,+ label,+ applicationUniversalIdentifier: CUSTOM,+ },+ i18nContext: buildI18nContext({+ isStandardApp: false,+ ownerApplicationUniversalIdentifier: CUSTOM,+ i18nInstance,+ }),+ }).label;++ it("translates a custom entity's label through Twenty's catalog when it has no catalog of its own", () => {+ const messageId = generateMessageId('Company', 'fieldMetadata.label');+expect(- resolveEffectiveTranslatedFlatEntity({- metadataName: 'fieldMetadata',- flatEntity: {- ...flatFieldMetadata,- applicationUniversalIdentifier: CUSTOM,- },- i18nContext: buildI18nContext({- isStandardApp: false,- ownerApplicationUniversalIdentifier: CUSTOM,- }),- }).label,- ).toBe('Company');+ resolveCustomLabel('Company', { _: (id: string) => `translated:${id}` }),+ ).toBe(`translated:${messageId}`);+ });++ it("keeps a custom label that Twenty's catalog does not know", () => {+ expect(+ resolveCustomLabel('Contract renewal', { _: (id: string) => id }),+ ).toBe('Contract renewal');+ });++ it("translates a custom object's system field label with Twenty's real catalog", () => {+ const i18n = setupI18n({+ locale: 'fr-FR',+ messages: { 'fr-FR': frenchMessages },+ });+ const translated = resolveCustomLabel('Creation date', i18n);++ expect(translated).toBe(+ i18n._(generateMessageId('Creation date', 'fieldMetadata.label')),+ );+ expect(translated).not.toBe('Creation date');});});diff --git a/packages/twenty-server/src/engine/metadata-modules/overrides/utils/resolve-effective-entity-property.util.ts b/packages/twenty-server/src/engine/metadata-modules/overrides/utils/resolve-effective-entity-property.util.tsindex 975a91733d8..30105de2c85 100644--- a/packages/twenty-server/src/engine/metadata-modules/overrides/utils/resolve-effective-entity-property.util.ts+++ b/packages/twenty-server/src/engine/metadata-modules/overrides/utils/resolve-effective-entity-property.util.ts@@ -86,10 +86,15 @@ const resolveEffectiveProperty = ({}}- // Custom (non-standard) entities without a catalog have no standard label- // to resolve or translate, and property renames live in base columns.if (!isStandardApp && !isDefined(applicationCatalog)) {- return safeBaseValue;+ return isTranslatable && !isDefined(overrideValue)+ ? translateStandardLabel({+ sourceValue: safeBaseValue,+ context: `${metadataName}.${property}`,+ applicationCatalog,+ i18nInstance,+ })+ : safeBaseValue;}if (!isTranslatable && isDefined(overrideValue)) {@@ -103,7 +108,6 @@ const resolveEffectiveProperty = ({return translateStandardLabel({sourceValue: safeBaseValue,context: `${metadataName}.${property}`,- isStandardApp,applicationCatalog,i18nInstance,});diff --git a/packages/twenty-server/src/engine/subscriptions/metadata-event/utils/__tests__/resolve-metadata-event-record.util.spec.ts b/packages/twenty-server/src/engine/subscriptions/metadata-event/utils/__tests__/resolve-metadata-event-record.util.spec.tsindex 340d17f5ef2..390b3bcae45 100644--- a/packages/twenty-server/src/engine/subscriptions/metadata-event/utils/__tests__/resolve-metadata-event-record.util.spec.ts+++ b/packages/twenty-server/src/engine/subscriptions/metadata-event/utils/__tests__/resolve-metadata-event-record.util.spec.ts@@ -73,13 +73,14 @@ describe('resolveMetadataEventRecord', () => {expect(resolved.shortLabel).toBeNull();});- it('should return custom-application values untouched when no catalog applies', () => {+ it("should keep custom-application values that Twenty's catalog does not know", () => {const resolved = resolveMetadataEventRecord({metadataName: 'view',record: { name: 'My custom view' },i18nContext: buildI18nContext({isStandardApp: false,applicationCatalog: undefined,+ i18nInstance: { _: (messageId: string) => messageId },}),});