-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Translations: Add script to convert from translation template data to…
… TypeScript
- Loading branch information
1 parent
095cfe1
commit 96762b4
Showing
5 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
|
||
// Converts a response to the GitHub translation form to | ||
// possible contents of a TypeScript file. | ||
|
||
import { | ||
createInterface as createReadlineInterface, Interface as ReadlineInterface | ||
} from 'node:readline/promises'; | ||
import { stdin, stderr } from 'node:process'; | ||
|
||
const readAllInput = (inputReader: ReadlineInterface): Promise<string> => { | ||
const lines: string[] = []; | ||
inputReader.on('line', line => { | ||
lines.push(line); | ||
}); | ||
|
||
return new Promise(resolve => { | ||
inputReader.on('close', () => { | ||
resolve(lines.join('\n')); | ||
}); | ||
}); | ||
}; | ||
|
||
const main = async () => { | ||
const inputReader = createReadlineInterface({ input: stdin, output: stderr }); | ||
inputReader.prompt(); | ||
|
||
const input = await readAllInput(inputReader); | ||
|
||
const data: Record<string, string> = {}; | ||
|
||
let nextKey: string|null = null; | ||
for (const line of input.split('\n')) { | ||
if (!nextKey) { | ||
nextKey = line; | ||
continue; | ||
} else if (line.trim() === '') { | ||
continue; | ||
} | ||
|
||
const value = line; | ||
const currentKey = nextKey; | ||
nextKey = null; | ||
|
||
if (value !== 'No response') { | ||
data[currentKey] = value; | ||
} | ||
} | ||
|
||
const lang = data.Language; | ||
delete data.Language; | ||
|
||
const translationLines: string[] = []; | ||
for (let key in data) { | ||
key = key.replace(/[^0-9a-zA-Z_]/g, '?'); | ||
if (key.startsWith('_')) { | ||
throw new Error('Invalid key, ' + key); | ||
} | ||
|
||
let value = data[key]; | ||
if (!value.match(/^(?:function|\(.*=>)/)) { | ||
value = JSON.stringify(value); | ||
} | ||
|
||
translationLines.push(`\t${key}: ${value},`); | ||
} | ||
|
||
const tsFileData = [ | ||
'import { defaultEditorLocalization, EditorLocalization } from \'../localization\';', | ||
'throw new Error(\'This file needs to be manually verified -- translations can contain JavaScript\');', | ||
`// ${lang} localization`, | ||
'const localization: EditorLocalization = {', | ||
'\t...defaultEditorLocalization,', | ||
...translationLines, | ||
'}', | ||
]; | ||
|
||
console.log(tsFileData.join('\n')); | ||
}; | ||
|
||
void main(); |