Skip to content

Commit

Permalink
fix: ensure shared strings are in test sim
Browse files Browse the repository at this point in the history
For #361.
  • Loading branch information
liammulh committed Feb 8, 2023
1 parent 64cdee7 commit 114d8b1
Showing 1 changed file with 67 additions and 27 deletions.
94 changes: 67 additions & 27 deletions src/server/translationApi/getReplacementStringObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,42 @@

import getLocaleInfo from './getLocaleInfo.js';
import getRepoNameFromStringKeyWithRepoName from './getRepoNameFromStringKeyWithRepoName.js';
import getSimMetadata from './getSimMetadata.js';
import getSimNamesAndTitles from './getSimNamesAndTitles.js';
import getStringKeyFromStringKeyWithRepoName from './getStringKeyFromStringKeyWithRepoName.js';
import logger from './logger.js';

/**
* If a key is translated, add embedding marks to it.
*
* @param {Object} stringsObject - the translation form data object, either sim-specific, shared, or common
* @param stringKeyWithoutDots - string key with dots/periods replaced with _DOT_
* @param stringKeysWithRepoName - object of REPO_NAME/stringKey: value
* @param stringKeyWithRepoName - REPO_NAME/stringKey
* @param direction - language direction; either left-to-right or right-to-left
*/
const addEmbeddingMarks = (
stringsObject,
stringKeyWithoutDots,
stringKeysWithRepoName,
stringKeyWithRepoName,
direction
) => {
const keyIsPresent = Object.keys( stringsObject ).includes( stringKeyWithoutDots );
const keyIsTranslated = keyIsPresent ? stringsObject[ stringKeyWithoutDots ].translated !== '' : false;
if ( keyIsTranslated ) {

// To understand why we're adding embedding marks, see https://github.com/phetsims/rosetta/issues/27.
const ltrMark = '\u202a';
const rtlMark = '\u202b';
const endDirectionalMark = '\u202c';
const translatedValue = stringsObject[ stringKeyWithoutDots ].translated;
stringKeysWithRepoName[ stringKeyWithRepoName ] = direction === 'rtl' ?
rtlMark + translatedValue + endDirectionalMark :
ltrMark + translatedValue + endDirectionalMark;
}
};

/**
* Return the replacement string object for a given sim and in-progress translation.
*
Expand All @@ -26,6 +59,14 @@ const getReplacementStringObject = async ( stringKeysWithRepoName, translation )
const localeInfo = await getLocaleInfo();
const direction = localeInfo[ translation.locale ].direction;

// Get list of sim names for checking if we're dealing with shared strings. Note how
// we're passing true for the second argument. This is to say we're a team member, so
// please get all the sims, even the ones that wouldn't normally be visible, e.g. Bumper.
// For context on this, see https://github.com/phetsims/rosetta/issues/360 and
// https://github.com/phetsims/rosetta/issues/361.
const simMetadata = await getSimMetadata();
const simNames = Object.keys( await getSimNamesAndTitles( simMetadata, 'true' ) );

for ( const stringKeyWithRepoName of Object.keys( stringKeysWithRepoName ) ) {
const repoName = getRepoNameFromStringKeyWithRepoName( stringKeyWithRepoName );
const stringKey = getStringKeyFromStringKeyWithRepoName( stringKeyWithRepoName );
Expand All @@ -34,41 +75,40 @@ const getReplacementStringObject = async ( stringKeysWithRepoName, translation )

// We have a sim-specific repo.

// Add translated key if it has been translated.
const simSpecificObject = translation.translationFormData.simSpecific;
const keyIsPresent = Object.keys( simSpecificObject ).includes( stringKeyWithoutDots );
const keyIsTranslated = keyIsPresent ? simSpecificObject[ stringKeyWithoutDots ].translated !== '' : false;
if ( keyIsTranslated ) {
addEmbeddingMarks(
simSpecificObject,
stringKeyWithoutDots,
stringKeysWithRepoName,
stringKeyWithRepoName,
direction
);
}
else if ( simNames.includes( repoName ) ) {

// We have a shared repo.

// To understand why we're adding embedding marks, see https://github.com/phetsims/rosetta/issues/27.
const ltrMark = '\u202a';
const rtlMark = '\u202b';
const endDirectionalMark = '\u202c';
const translatedValue = simSpecificObject[ stringKeyWithoutDots ].translated;
stringKeysWithRepoName[ stringKeyWithRepoName ] = direction === 'rtl' ?
rtlMark + translatedValue + endDirectionalMark :
ltrMark + translatedValue + endDirectionalMark;
}
const sharedObject = translation.translationFormData.shared;
addEmbeddingMarks(
sharedObject,
stringKeyWithoutDots,
stringKeysWithRepoName,
stringKeyWithRepoName,
direction
);
}
else {

// We have a common repo.

// Add translated key if it has been translated.
const commonObject = translation.translationFormData.common;
const keyIsPresent = Object.keys( commonObject ).includes( stringKeyWithoutDots );
const keyIsTranslated = keyIsPresent ? commonObject[ stringKeyWithoutDots ].translated !== '' : false;
if ( keyIsTranslated ) {

// To understand why we're adding embedding marks, see https://github.com/phetsims/rosetta/issues/27.
const ltrMark = '\u202a';
const rtlMark = '\u202b';
const endDirectionalMark = '\u202c';
const translatedValue = commonObject[ stringKeyWithoutDots ].translated;
stringKeysWithRepoName[ stringKeyWithRepoName ] = direction === 'rtl' ?
rtlMark + translatedValue + endDirectionalMark :
ltrMark + translatedValue + endDirectionalMark;
}
addEmbeddingMarks(
commonObject,
stringKeyWithoutDots,
stringKeysWithRepoName,
stringKeyWithRepoName,
direction
);
}
}
logger.info( 'got replacement string object; returning it' );
Expand Down

0 comments on commit 114d8b1

Please sign in to comment.