|
| 1 | +import os |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | + |
| 8 | +project_root = Path(__file__).parent.parent.parent |
| 9 | +locale_file = project_root / "src" / "locale.ts" |
| 10 | +ant_locale_file = project_root / "development" / "src" / "ant-phone" / "locale.ts" |
| 11 | + |
| 12 | + |
| 13 | +with open(locale_file, 'r') as f: |
| 14 | + content = f.read() |
| 15 | + |
| 16 | +locale_pattern = r'^export const (\w+) = \{' |
| 17 | +existing_locales = set(re.findall(locale_pattern, content, re.MULTILINE)) |
| 18 | + |
| 19 | +with open(ant_locale_file, 'r') as f: |
| 20 | + content = f.read() |
| 21 | + |
| 22 | +import_pattern = r'^import (\w+) from "antd/es/locale/' |
| 23 | +antd_locales = set(re.findall(import_pattern, content, re.MULTILINE)) |
| 24 | + |
| 25 | +try: |
| 26 | + result = subprocess.run( |
| 27 | + ['node', '-e', 'const locale = require("@mui/material/locale"); console.log(Object.keys(locale).join(","))'], |
| 28 | + cwd=project_root / "development", |
| 29 | + capture_output=True, |
| 30 | + text=True, |
| 31 | + timeout=10 |
| 32 | + ) |
| 33 | + if result.returncode == 0 and result.stdout.strip(): |
| 34 | + mui_locales = set(result.stdout.strip().split(',')) |
| 35 | + else: |
| 36 | + mui_locales = existing_locales |
| 37 | +except: |
| 38 | + mui_locales = existing_locales |
| 39 | + |
| 40 | +missing_from_antd = antd_locales - existing_locales |
| 41 | +missing_from_mui = mui_locales - existing_locales |
| 42 | +all_missing = missing_from_antd | missing_from_mui |
| 43 | + |
| 44 | +if all_missing: |
| 45 | + issue_body = "## Missing Locale Translations\n\n" |
| 46 | + issue_body += "This automated check has detected missing locale files in `react-phone-hooks` that are available in dependent packages.\n\n" |
| 47 | + issue_body += "### Summary\n\n" |
| 48 | + issue_body += f"**Total missing locales: {len(all_missing)}**\n\n" |
| 49 | + |
| 50 | + missing_locales = {} |
| 51 | + if missing_from_antd: |
| 52 | + missing_locales['antd'] = missing_from_antd |
| 53 | + if missing_from_mui: |
| 54 | + missing_locales['mui'] = missing_from_mui |
| 55 | + |
| 56 | + for source, locales in missing_locales.items(): |
| 57 | + if locales: |
| 58 | + issue_body += f"### Missing from {source}\n\n" |
| 59 | + for locale in sorted(locales): |
| 60 | + issue_body += f"- `{locale}`\n" |
| 61 | + issue_body += "\n" |
| 62 | + |
| 63 | + issue_body += "### Action Required\n\n" |
| 64 | + issue_body += "Please add translation entries for the missing locales listed above. Each locale should include:\n" |
| 65 | + issue_body += "- `searchNotFound`: Translation for \"Country not found\"\n" |
| 66 | + issue_body += "- `searchPlaceholder`: Translation for \"Search country\"\n" |
| 67 | + issue_body += "- `countries`: Object mapping English country names to translations\n\n" |
| 68 | + issue_body += "### Example Structure\n\n" |
| 69 | + issue_body += "```typescript\n" |
| 70 | + issue_body += "export const enUS = {\n" |
| 71 | + issue_body += " searchNotFound: \"Country not found\",\n" |
| 72 | + issue_body += " searchPlaceholder: \"Search country\",\n" |
| 73 | + issue_body += " countries: {\n" |
| 74 | + issue_body += " \"United States\": \"United States\",\n" |
| 75 | + issue_body += " \"United Kingdom\": \"United Kingdom\",\n" |
| 76 | + issue_body += " },\n" |
| 77 | + issue_body += "}\n" |
| 78 | + issue_body += "```\n\n" |
| 79 | + issue_body += "---\n" |
| 80 | + issue_body += "*This issue was automatically generated by the locale detection script.*\n" |
| 81 | + |
| 82 | + output_file = Path("/tmp/missing_locales_issue.md") |
| 83 | + with open(output_file, 'w') as f: |
| 84 | + f.write(issue_body) |
| 85 | + |
| 86 | + if os.getenv('GITHUB_OUTPUT'): |
| 87 | + with open(os.getenv('GITHUB_OUTPUT'), 'a') as f: |
| 88 | + f.write(f"has_missing=true\n") |
| 89 | + f.write(f"count={len(all_missing)}\n") |
| 90 | + |
| 91 | + sys.exit(1) |
| 92 | +else: |
| 93 | + if os.getenv('GITHUB_OUTPUT'): |
| 94 | + with open(os.getenv('GITHUB_OUTPUT'), 'a') as f: |
| 95 | + f.write(f"has_missing=false\n") |
| 96 | + f.write(f"count=0\n") |
| 97 | + |
| 98 | + sys.exit(0) |
0 commit comments