Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

reset-translated-file.js 2.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  1. #!/usr/bin/env node
  2. // [start-readme]
  3. //
  4. // This is a convenience script for replacing the contents of translated
  5. // files with the English content from their corresponding source file.
  6. //
  7. // It's intended to be a workaround to temporarily bypass Crowdin parser bugs
  8. // while we wait for translators to fix them.
  9. //
  10. // Usage:
  11. // script/reset-translated-file.js <filename>
  12. //
  13. // Examples:
  14. //
  15. // $ script/reset-translated-file.js translations/es-XL/content/actions/index.md
  16. //
  17. // [end-readme]
  18. const program = require('commander')
  19. const { execSync } = require('child_process')
  20. const assert = require('assert')
  21. const fs = require('fs')
  22. const path = require('path')
  23. const chalk = require('chalk')
  24. program
  25. .description('reset translated files')
  26. .option('-m, --prefer-main', 'Reset file to the translated file, try using the file from `main` branch first, if not found (usually due to renaming), fall back to English source.')
  27. .parse(process.argv)
  28. const resetToEnglishSource = (translationFilePath) => {
  29. assert(translationFilePath.startsWith('translations/'), 'path argument must be in the format `translations/<lang>/path/to/file`')
  30. assert(fs.existsSync(translationFilePath), `file does not exist: ${translationFilePath}`)
  31. const relativePath = translationFilePath.split(path.sep).slice(2).join(path.sep)
  32. const englishFile = path.join(process.cwd(), relativePath)
  33. assert(fs.existsSync(englishFile), `file does not exist: ${englishFile}`)
  34. // replace file with English source
  35. const englishContent = fs.readFileSync(englishFile, 'utf8')
  36. fs.writeFileSync(translationFilePath, englishContent)
  37. console.log('-> reverted to English: %s', path.relative(process.cwd(), translationFilePath))
  38. }
  39. const [pathArg] = program.args
  40. assert(pathArg, 'first arg must be a target filename')
  41. // Is the arg a fully-qualified path?
  42. const relativePath = fs.existsSync(pathArg)
  43. ? path.relative(process.cwd(), pathArg)
  44. : pathArg
  45. if (program.preferMain) {
  46. try {
  47. execSync(`git checkout main -- ${relativePath}`, { stdio: 'pipe' })
  48. console.log('-> reverted to file from main branch: %s', relativePath)
  49. } catch (e) {
  50. if (e.message.includes('pathspec')) {
  51. console.warn(chalk.red(`cannot find ${relativePath} in main branch (likely because it was renamed); falling back to English source file.`))
  52. resetToEnglishSource(relativePath)
  53. } else {
  54. console.warn(e.message)
  55. }
  56. }
  57. } else {
  58. resetToEnglishSource(relativePath)
  59. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...