Skip to content

Commit

Permalink
Merge pull request #4154 from mathesar-foundation/i18n_scripts
Browse files Browse the repository at this point in the history
Removes unused i18n strings and adds script to detect them
  • Loading branch information
seancolsen authored Jan 22, 2025
2 parents d7c4871 + 243e7c8 commit 9e035ac
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 149 deletions.
2 changes: 1 addition & 1 deletion DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ Django uses gettext, which require the `.po` files to be compiled into a more ef
- If you encounter merge conflicts in `en/dict.json`, run this script to automatically resolve them:
```
python3 mathesar_ui/src/i18n/scripts/resolve_dict_merge_conflicts.py
python3 mathesar_ui/scripts/i18n/resolve_dict_merge_conflicts.py
```
## Translation process
Expand Down
56 changes: 56 additions & 0 deletions mathesar_ui/scripts/i18n/check_usused_strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3

import re
import os
import subprocess
from pathlib import Path
import json

MATHESAR_UI_DIR = Path(__file__).resolve().parent.parent.parent

EN_DICT_FILE = os.path.join(MATHESAR_UI_DIR, 'src/i18n/languages/en/dict.json')


# Checks for usage of `$_('string'` or `get(_)('string'`.
# Scenarios handled:
# - Direct usage: eg., `$_('string')`
# - Usage with args: eg., `$_('string', { values: { date: dateString } })`
# - Considers multiple lines. Eg.,
# ```
# $_(
# 'string',
# )
# ```
def grep_i18n_string(string):
try:
escaped_string = re.escape(string)
pattern = rf"\$_\(\s*'{escaped_string}'|get\(_\)\(\s*'{escaped_string}'"
subprocess.run(
[
'grep',
'-qrEz',
pattern,
'--include=**.svelte',
'--include=**.ts',
r'--exclude=\*i18n\*',
os.path.join(MATHESAR_UI_DIR, 'src')
],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
return True
except subprocess.CalledProcessError:
return False
except Exception:
raise


with open(EN_DICT_FILE, 'r') as en_dict_file:
en_dict_json = json.load(en_dict_file)
not_found_count = 0
for key in en_dict_json:
if not grep_i18n_string(key):
print("NOT FOUND: " + key)
not_found_count += 1
print("Number of strings not found: " + str(not_found_count))
Loading

0 comments on commit 9e035ac

Please sign in to comment.