Skip to content

Commit

Permalink
code review fixes in service host and renaming in model
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolie Rabideau authored and Jolie Rabideau committed Jan 22, 2024
1 parent 383e0f6 commit 9f247e4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 38 deletions.
52 changes: 20 additions & 32 deletions src/main/services/localization.service-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ import { joinUriPaths } from '@node/utils/util';

const LOCALIZATION_ROOT_URI = joinUriPaths('resources://', 'assets', 'localization');
const LANGUAGE_CODE_REGEX = /\\([a-zA-Z]+)\.json/;
const DEFAULT_LANGUAGE = 'eng';

function getLanguageCodeFromUri(uriToMatch: string): string {
const isMatch = LANGUAGE_CODE_REGEX.test(uriToMatch);
const match = isMatch ? uriToMatch.match(LANGUAGE_CODE_REGEX) : undefined;
if (match) return match[1];
throw new Error('Localization service - No match for language code');
const match = uriToMatch.match(LANGUAGE_CODE_REGEX);
if (!match) throw new Error('Localization service - No match for language code');
return match[1];
}

/** Convert contents of a specific localization json file to an object */
function convertToLocalizationData(jsonString: string, languageCode: string): LocalizationData {
const ld: LocalizationData = deserialize(jsonString);
if (typeof ld !== 'object')
const locationData: LocalizationData = deserialize(jsonString);
if (typeof locationData !== 'object')
throw new Error(`Localization data for language '${languageCode}' is invalid`);
return ld;
return locationData;
}

async function getLocalizedFileUris(): Promise<string[]> {
const entries = await nodeFS.readDir(LOCALIZATION_ROOT_URI);
if (entries) return entries.file;
throw new Error('No entries found in localization folder');
if (!entries) throw new Error('No entries found in localization folder');
return entries.file;
}

// Map of ISO 639-2 code to localized values for that language
/** Map of ISO 639-2 code to localized values for that language */
const languageLocalizedData = new Map<string, LocalizationData>();

/** Load the contents of all localization files from disk */
Expand Down Expand Up @@ -76,38 +76,26 @@ async function initialize(): Promise<void> {
return initializationPromise;
}

const DEFAULT_LANGUAGE = 'eng';

async function getLocalizedValueForKey(localizeKey: string, language: string = DEFAULT_LANGUAGE) {
async function getLocalizedString(localizeKey: string, language: string = DEFAULT_LANGUAGE) {
await initialize();
const languageData = languageLocalizedData?.get(language);

if (languageData && languageData[localizeKey]) {
return languageData[localizeKey];
}
throw new Error('Missing/invalid localization data');
if (!languageData || !languageData[localizeKey])
throw new Error('Missing/invalid localization data');
return languageData[localizeKey];
}

async function getLocalizedValuesForKeys(
localizeKeys: string[],
language: string = DEFAULT_LANGUAGE,
) {
async function getLocalizedStrings(localizeKeys: string[], language: string = DEFAULT_LANGUAGE) {
await initialize();
const retVal: LocalizationData = {};
const languageData = languageLocalizedData?.get(language);
const languageData = languageLocalizedData.get(language);

if (languageData) {
localizeKeys.forEach((key) => {
retVal[key] = languageData[key];
});
return retVal;
}
throw new Error('Missing/invalid localization data');
if (!languageData) throw new Error('Missing/invalid localization data');
return Object.fromEntries(localizeKeys.map((key) => [key, languageData[key]]));
}

const localizationService: LocalizationServiceType = {
getLocalizedValueForKey,
getLocalizedValuesForKeys,
getLocalizedString,
getLocalizedStrings,
};

/** Register the network object that backs the PAPI localization service */
Expand Down
4 changes: 2 additions & 2 deletions src/shared/services/localization.service-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export interface LocalizationServiceType {
* @param language ISO 639-2 code for the language, defaults to 'eng' if unspecified
* @returns Localized string
*/
getLocalizedValueForKey: (localizeKey: string, language?: string) => Promise<string>;
getLocalizedString: (localizeKey: string, language?: string) => Promise<string>;
/**
* Look up localized strings for all localizeKeys provided
*
* @param localizeKeys Array of localize keys that correspond to localized values
* @param language ISO 639-2 code for the language, defaults to 'eng' if unspecified
* @returns Object whose keys are localizeKeys and values are localized strings
*/
getLocalizedValuesForKeys: (
getLocalizedStrings: (
localizeKeys: string[],
language?: string,
) => Promise<{ [localizeKey: string]: string }>;
Expand Down
8 changes: 4 additions & 4 deletions src/shared/services/localization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ async function initialize(): Promise<void> {
}

const localizationService: LocalizationServiceType = {
getLocalizedValueForKey: async (localizeKey: string, language?: string) => {
getLocalizedString: async (localizeKey: string, language?: string) => {
await initialize();
return networkObject.getLocalizedValueForKey(localizeKey, language);
return networkObject.getLocalizedString(localizeKey, language);
},
getLocalizedValuesForKeys: async (localizeKeys: string[], language?: string) => {
getLocalizedStrings: async (localizeKeys: string[], language?: string) => {
await initialize();
return networkObject.getLocalizedValuesForKeys(localizeKeys, language);
return networkObject.getLocalizedStrings(localizeKeys, language);
},
};

Expand Down

0 comments on commit 9f247e4

Please sign in to comment.