Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion scripts/i18n-sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
MAX_CHARS = 8000
PLACEHOLDERS = re.compile(r"%(\d)")
URL_RE = re.compile(r"https?://[^\s)]+")
ASCII_PREFIX_RE = re.compile(r"[\x21-\x7e]*")
HEADING_RE = re.compile(r"^#{1,6} ", re.M)


Expand Down Expand Up @@ -517,13 +518,21 @@ def protected_inline(text):
return Counter(kept)


def urls_of(text):
# A bare URL that ends a sentence picks up its full stop, which a
# translation may move or write as 。 or । right after it. The URLs are
# ASCII, so one ends at the first character beyond that, and a trailing
# ASCII full stop or comma is the sentence's, not the URL's.
return [ASCII_PREFIX_RE.match(url).group().rstrip(".,;:!?") for url in URL_RE.findall(text)]
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved


def validate_manual(source, value):
from collections import Counter
if not isinstance(value, str) or not value.strip():
raise SyncError("empty translation")
if len(HEADING_RE.findall(source)) != len(HEADING_RE.findall(value)):
raise SyncError("heading count changed")
if sorted(URL_RE.findall(source)) != sorted(URL_RE.findall(value)):
if sorted(urls_of(source)) != sorted(urls_of(value)):
raise SyncError("URLs changed")
if Counter(FENCE_RE.findall(source)) != Counter(FENCE_RE.findall(value)):
raise SyncError("code block contents changed")
Expand Down
8 changes: 7 additions & 1 deletion test/shell.d/i18n-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ const englishTexts = {}
for (const file of englishManual) englishTexts[file] = fs.readFileSync(path.join(manualDir, file), 'utf8')
const bagOf = (items) => JSON.stringify([...items].sort())
const fencesOf = (text) => [...text.matchAll(/```[^\n]*\n([\s\S]*?)```/g)].map((m) => m[1])
const urlsOf = (text) => text.match(/https?:\/\/[^\s)]+/g) || []
// A bare URL that ends a sentence picks up its full stop, which a translation
// may move or write as 。 or । right after it. The URLs are ASCII, so one ends
// at the first character beyond that, and a trailing ASCII full stop or comma
// is the sentence's, not the URL's.
const urlsOf = (text) => (text.match(/https?:\/\/[^\s)]+/g) || []).map((url) => url.match(/^[\x21-\x7e]*/)[0].replace(/[.,;:!?]+$/, ''))
const relDestsOf = (text) => [...text.matchAll(/\]\((?!<?https?:\/\/)([^)\s]+)/g)].map((m) => m[1])
const protectedInlineOf = (text) => [...text.matchAll(/`([^`\n]+)`/g)].map((m) => m[1])
.filter((span) => /[/~$]/.test(span) || span.startsWith('omarchy ') || span.startsWith('omarchy-'))
Expand All @@ -199,6 +203,8 @@ const fencedChapter = englishManual.find((file) => englishTexts[file].includes('
assert(manualProblems(englishTexts[fencedChapter], englishTexts[fencedChapter].replace(/```[^\n]*\n/, '$&echo tampered\n')).length > 0, 'the manual integrity check catches a changed code block')
const linkedChapter = englishManual.find((file) => /\]\(\d\d-[a-z0-9-]+\.md\)/.test(englishTexts[file]))
assert(manualProblems(englishTexts[linkedChapter], englishTexts[linkedChapter].replace(/\]\(\d\d-[a-z0-9-]+\.md\)/, '](99-missing.md)')).length > 0, 'the manual integrity check catches a broken chapter link')
assert(['https://example.org/walls を参照してください。', '参照: https://example.org/walls。', 'देखें https://example.org/walls।', 'دیکھیں https://example.org/walls۔']
.every((translated) => manualProblems('See https://example.org/walls.', translated).length === 0) && manualProblems('See https://example.org/walls.', 'See https://example.org/other.').length > 0, 'the manual integrity check ignores the full stop after a URL, not the URL')
for (const code of codes) {
if (code === 'en') continue
const dir = path.join(manualDir, code)
Expand Down
Loading