( dictionariesDir: string, targetDictionariesDir: string )
| 96 | * @returns {Object} Object mapping English comment text to translations in all languages |
| 97 | */ |
| 98 | export function createCommentMap( |
| 99 | dictionariesDir: string, |
| 100 | targetDictionariesDir: string |
| 101 | ): CommentDictionary { |
| 102 | log( |
| 103 | `Creating comment map from ${dictionariesDir} and ${targetDictionariesDir}` |
| 104 | ); |
| 105 | const languages = readdirSync(targetDictionariesDir); |
| 106 | |
| 107 | const dictionaries = languages.reduce((acc, lang) => { |
| 108 | const commentsPath = resolve(targetDictionariesDir, lang, 'comments.json'); |
| 109 | const commentsData = JSON.parse(readFileSync(commentsPath, 'utf8')); |
| 110 | return { |
| 111 | ...acc, |
| 112 | [lang]: commentsData |
| 113 | }; |
| 114 | }, {}); |
| 115 | |
| 116 | const COMMENTS_TO_TRANSLATE = JSON.parse( |
| 117 | readFileSync(resolve(dictionariesDir, 'english', 'comments.json'), 'utf8') |
| 118 | ) as Record<string, string>; |
| 119 | |
| 120 | const COMMENTS_TO_NOT_TRANSLATE = JSON.parse( |
| 121 | readFileSync( |
| 122 | resolve(dictionariesDir, 'english', 'comments-to-not-translate.json'), |
| 123 | 'utf8' |
| 124 | ) |
| 125 | ) as Record<string, string>; |
| 126 | |
| 127 | // map from english comment text to translations |
| 128 | const translatedCommentMap = Object.entries(COMMENTS_TO_TRANSLATE).reduce( |
| 129 | (acc, [id, text]) => { |
| 130 | return { |
| 131 | ...acc, |
| 132 | [text]: getTranslationEntry(dictionaries, { engId: id, text }) |
| 133 | }; |
| 134 | }, |
| 135 | {} as CommentDictionary |
| 136 | ); |
| 137 | |
| 138 | // map from english comment text to itself |
| 139 | const untranslatableCommentMap = Object.values( |
| 140 | COMMENTS_TO_NOT_TRANSLATE |
| 141 | ).reduce((acc, text) => { |
| 142 | const englishEntry = languages.reduce( |
| 143 | (acc, lang) => ({ |
| 144 | ...acc, |
| 145 | [lang]: text |
| 146 | }), |
| 147 | {} |
| 148 | ); |
| 149 | return { |
| 150 | ...acc, |
| 151 | [text]: englishEntry |
| 152 | }; |
| 153 | }, {} as CommentDictionary); |
| 154 | |
| 155 | const allComments = { ...translatedCommentMap, ...untranslatableCommentMap }; |
no test coverage detected