使用腳本驗證不同語言版本之間的 JSON-LD 一致性
Validating JSON-LD Consistency Across Language Versions With a Script
多語言網站可用腳本檢查不同語言版本的 JSON-LD 是否存在資料不一致。腳本比對 url、logo、telephone、email 和 sameAs,並略過預期會因語言而異的 description;可在每次部署後於 CI 執行,或每週透過 cron job 執行。保持網站資料一致,有助 AI 助理準確掌握機構資訊。
Multilingual sites often have structured data that drifts between languages. The English page lists a phone number, the Japanese page has an old one, the Chinese page is missing the address entirely. Search engines and AI assistants read these as conflicting facts about the same organisation. A short script can catch the drift before it spreads.
What to compare
Some fields should be identical across languages:
- Organisation
name(or a deliberate, documented localised name) -
url,logo,telephone,email -
addresscomponents -
sameAsprofile links
Other fields are expected to differ, such as description. The script should compare the first group and ignore the second.
Extracting JSON-LD
import json, re, requests
def jsonld(url):
html = requests.get(url, timeout=20, headers={"User-Agent": "schema-check"}).text
blocks = re.findall(r'<script[^>]+application/ld\+json[^>]*>(.*?)</script>', html, re.S)
out = []
for b in blocks:
try:
data = json.loads(b)
except json.JSONDecodeError:
print("invalid JSON-LD on", url)
continue
out.extend(data if isinstance(data, list) else [data])
return out
def org(items):
for it in items:
if it.get("@type") in ("Organization", "LocalBusiness", "ProfessionalService"):
return it
return {}
Comparing versions
FIELDS = ["url", "logo", "telephone", "email", "sameAs"]
pages = {
"en": "https://example.com/en/about/",
"zh": "https://example.com/zh/about/",
"ja": "https://example.com/ja/about/",
}
ref = org(jsonld(pages["en"]))
for lang, url in pages.items():
o = org(jsonld(url))
for f in FIELDS:
a, b = ref.get(f), o.get(f)
if isinstance(a, list): a, b = sorted(a), sorted(b or [])
if a != b:
print(f"[{lang}] {f}: {b!r} != en {a!r}")
Run it in CI after each deploy, or weekly from a cron job.
Common findings
-
Phone formats. One version uses
+852 ..., another uses local format without the country code. Pick international format everywhere. - Stale sameAs links. A social profile that was renamed on one language version only.
- Missing blocks. Translated templates that dropped the script tag entirely.
- Invalid JSON. Translators editing a template and breaking a quote or comma.
Why it matters for AI search
Assistants build an internal picture of who a company is from many sources. Your own site should be the most consistent of them. If it disagrees with itself across languages, it is harder for a model to state your details with confidence.
For the rest of the multilingual checklist, including hreflang and URL structure, see our multilingual SEO guide.
Published by PandaWM, an overseas marketing team based in Hong Kong.
來源:DEV Community · dev.to