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

fix-translation-errors.js 3.0 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  1. const { execSync } = require('child_process')
  2. const { get, set } = require('lodash')
  3. const fs = require('fs')
  4. const path = require('path')
  5. const fm = require('../lib/frontmatter')
  6. const matter = require('gray-matter')
  7. const chalk = require('chalk')
  8. const yaml = require('js-yaml')
  9. const ghesReleaseNotesSchema = require('../lib/release-notes-schema')
  10. const revalidator = require('revalidator')
  11. const fixableFmProps = ['type', 'changelog', 'mapTopic', 'hidden', 'layout', 'defaultPlatform', 'showMiniToc', 'allowTitleToDifferFromFilename', 'interactive', 'beta_product']
  12. const fixableYmlProps = ['date']
  13. // [start-readme]
  14. //
  15. // Run this script to fix known frontmatter errors by copying values from english file
  16. // Currently only fixing errors in: 'type', 'changelog'
  17. // Please double check the changes created by this script before committing.
  18. //
  19. // [end-readme]
  20. const loadAndValidateContent = async (path, schema) => {
  21. let fileContents
  22. try {
  23. fileContents = await fs.promises.readFile(path, 'utf8')
  24. } catch (e) {
  25. console.error(e.message)
  26. return null
  27. }
  28. if (path.endsWith('yml')) {
  29. let data; let errors = []
  30. try {
  31. data = yaml.safeLoad(fileContents)
  32. } catch {}
  33. if (data && schema) {
  34. ({ errors } = revalidator.validate(data, schema))
  35. }
  36. return { data, errors, content: null }
  37. } else {
  38. return fm(fileContents)
  39. }
  40. }
  41. const cmd = 'git diff --name-only origin/main | egrep "^translations/.*/(content/.+.md|data/release-notes/.*.yml)$"'
  42. const changedFilesRelPaths = execSync(cmd).toString().split('\n')
  43. changedFilesRelPaths.forEach(async (relPath) => {
  44. if (!relPath || relPath.endsWith('README.md')) return
  45. const localisedAbsPath = path.join(__dirname, '..', relPath)
  46. // find the corresponding english file by removing the first 2 path segments: /translation/<language code>
  47. const engAbsPath = path.join(__dirname, '..', relPath.split(path.sep).slice(2).join(path.sep))
  48. const localisedResult = await loadAndValidateContent(localisedAbsPath, ghesReleaseNotesSchema)
  49. if (!localisedResult) return
  50. const { data, errors, content } = localisedResult
  51. const fixableProps = relPath.endsWith('yml') ? fixableYmlProps : fixableFmProps
  52. const fixableErrors = errors.filter(({ property }) => {
  53. const prop = property.split('.')
  54. return fixableProps.includes(prop[0])
  55. })
  56. if (!data || fixableErrors.length === 0) return
  57. const engResult = await loadAndValidateContent(engAbsPath)
  58. if (!engResult) return
  59. const { data: engData } = engResult
  60. console.log(chalk.red('fixing errors in ') + chalk.bold(relPath))
  61. const newData = data
  62. fixableErrors.forEach(({ property }) => {
  63. const correctValue = get(engData, property)
  64. console.log(` [${property}]: ${get(data, property)} -> ${correctValue}`)
  65. set(newData, property, correctValue)
  66. })
  67. let toWrite
  68. if (content) {
  69. toWrite = matter.stringify(content, newData, { lineWidth: 10000, forceQuotes: true })
  70. } else {
  71. toWrite = yaml.safeDump(newData, { lineWidth: 10000, forceQuotes: true })
  72. }
  73. fs.writeFileSync(localisedAbsPath, toWrite)
  74. })
Tip!

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

Comments

Loading...