Skip to content

Commit

Permalink
Merge pull request #27310 from software-mansion-labs/ts-migration/tra…
Browse files Browse the repository at this point in the history
…nslations-follow-up

[No QA][TS migration] Improve translations: keep translations in sync
  • Loading branch information
cristipaval authored Sep 13, 2023
2 parents 6e3ab3d + 88525b3 commit 9675a77
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
16 changes: 12 additions & 4 deletions src/languages/en.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {CONST as COMMON_CONST} from 'expensify-common/lib/CONST';
import CONST from '../CONST';
import type {
Translation,
AddressLineParams,
CharacterLimitParams,
MaxParticipantsReachedParams,
Expand Down Expand Up @@ -75,6 +74,15 @@ import type {
} from './types';
import * as ReportActionsUtils from '../libs/ReportActionsUtils';

type StateValue = {
stateISO: string;
stateName: string;
};

type States = Record<keyof typeof COMMON_CONST.STATES, StateValue>;

type AllCountries = Record<keyof typeof CONST.ALL_COUNTRIES, string>;

/* eslint-disable max-len */
export default {
common: {
Expand Down Expand Up @@ -1662,8 +1670,8 @@ export default {
createAccount: 'Create A New Account',
logIn: 'Log In',
},
allStates: COMMON_CONST.STATES,
allCountries: CONST.ALL_COUNTRIES,
allStates: COMMON_CONST.STATES as States,
allCountries: CONST.ALL_COUNTRIES as AllCountries,
accessibilityHints: {
navigateToChatsList: 'Navigate back to chats list',
chatWelcomeMessage: 'Chat welcome message',
Expand Down Expand Up @@ -1757,4 +1765,4 @@ export default {
selectSuggestedAddress: 'Please select a suggested address',
},
},
} satisfies Translation;
};
4 changes: 2 additions & 2 deletions src/languages/es.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import CONST from '../CONST';
import * as ReportActionsUtils from '../libs/ReportActionsUtils';
import type {
Translation,
AddressLineParams,
CharacterLimitParams,
MaxParticipantsReachedParams,
Expand Down Expand Up @@ -72,6 +71,7 @@ import type {
SetTheRequestParams,
UpdatedTheRequestParams,
RemovedTheRequestParams,
EnglishTranslation,
} from './types';

/* eslint-disable max-len */
Expand Down Expand Up @@ -2249,4 +2249,4 @@ export default {
selectSuggestedAddress: 'Por favor, selecciona una dirección sugerida',
},
},
} satisfies Translation;
} satisfies EnglishTranslation;
8 changes: 4 additions & 4 deletions src/languages/translations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import en from './en';
import es from './es';
import esES from './es-ES';
import type {Translation, TranslationFlatObject} from './types';
import type {TranslationBase, TranslationFlatObject} from './types';

/**
* Converts an object to it's flattened version.
Expand All @@ -12,10 +12,10 @@ import type {Translation, TranslationFlatObject} from './types';
*/
// Necessary to export so that it is accessible to the unit tests
// eslint-disable-next-line rulesdir/no-inline-named-export
export function flattenObject(obj: Translation): TranslationFlatObject {
export function flattenObject(obj: TranslationBase): TranslationFlatObject {
const result: Record<string, unknown> = {};

const recursive = (data: Translation, key: string): void => {
const recursive = (data: TranslationBase, key: string): void => {
// If the data is a function or not a object (eg. a string or array),
// it's the final value for the key being built and there is no need
// for more recursion
Expand All @@ -27,7 +27,7 @@ export function flattenObject(obj: Translation): TranslationFlatObject {
// Recursive call to the keys and connect to the respective data
Object.keys(data).forEach((k) => {
isEmpty = false;
recursive(data[k] as Translation, key ? `${key}.${k}` : k);
recursive(data[k] as TranslationBase, key ? `${key}.${k}` : k);
});

// Check for when the object is empty but a key exists, so that
Expand Down
11 changes: 6 additions & 5 deletions src/languages/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ type UpdatedTheRequestParams = {valueName: string; newValueToDisplay: string; ol
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type TranslationBaseValue = string | string[] | ((...args: any[]) => string);

type Translation = {[key: string]: TranslationBaseValue | Translation};
type TranslationBase = {[key: string]: TranslationBaseValue | TranslationBase};

/* Flat Translation Object types */
// Flattens an object and returns concatenations of all the keys of nested objects
Expand All @@ -222,16 +222,17 @@ type TranslateType<TObject, TPath extends string> = TPath extends keyof TObject
: never
: never;

type TranslationsType = typeof en;
type EnglishTranslation = typeof en;

type TranslationPaths = FlattenObject<TranslationsType>;
type TranslationPaths = FlattenObject<EnglishTranslation>;

type TranslationFlatObject = {
[TKey in TranslationPaths]: TranslateType<TranslationsType, TKey>;
[TKey in TranslationPaths]: TranslateType<EnglishTranslation, TKey>;
};

export type {
Translation,
TranslationBase,
EnglishTranslation,
TranslationFlatObject,
AddressLineParams,
CharacterLimitParams,
Expand Down

0 comments on commit 9675a77

Please sign in to comment.