Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA][TS migration] Improve translations: keep translations in sync #27310

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should satisfies TranslationBase, shouldn't?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After testing, I agree. I'll add it in a separate PR 👍

};
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
Loading