-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1092 from activist-org/546-base-i18n-obj
#546 Base i18n map object and process to create it
- Loading branch information
Showing
6 changed files
with
1,145 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
""" | ||
Checks the i18nMap object to make sure that it's in sync with the base localization file. | ||
Usage: | ||
python3 frontend/i18n/check/i18n_check_map_object.py | ||
""" | ||
|
||
import json | ||
from collections.abc import MutableMapping | ||
from pathlib import Path | ||
|
||
# MARK: Load Base i18n | ||
|
||
i18n_check_dir = Path(__file__).parent.parent.resolve() | ||
with open(i18n_check_dir / "en-US.json", encoding="utf-8") as f: | ||
en_us_json_dict = json.loads(f.read()) | ||
|
||
flat_en_us_json_dict = {k: k for k, _ in en_us_json_dict.items()} | ||
|
||
# MARK: Load i18n Map | ||
|
||
frontend_types_dir = (Path(__file__).parent.parent.parent / "types").resolve() | ||
with open(frontend_types_dir / "i18n-map.ts", encoding="utf-8") as f: | ||
i18n_object_list = f.readlines() | ||
|
||
i18n_object_list_with_key_quotes = [] | ||
for i, line in enumerate(i18n_object_list): | ||
if i != 0 or i != len(i18n_object_list): | ||
if line.strip()[0] != '"': | ||
i18n_object_list_with_key_quotes.append( | ||
f'"{line.replace(":", '":').strip()}' | ||
) | ||
|
||
else: | ||
i18n_object_list_with_key_quotes.append( | ||
f"{line.replace(':', '":').strip()}" | ||
) | ||
|
||
i18n_object = ( | ||
"".join(i18n_object_list_with_key_quotes) | ||
.replace("export const i18nMap = ", "") | ||
.replace("};", "}") | ||
.replace('"{', "{") | ||
.replace('"}', "}") | ||
.replace(",}", "}") | ||
) | ||
i18n_object_dict = json.loads(i18n_object) | ||
|
||
|
||
# MARK: Flatten i18n Obj | ||
|
||
|
||
def flatten_nested_dict( | ||
dictionary: MutableMapping, parent_key: str = "", sep: str = "." | ||
) -> MutableMapping: | ||
""" | ||
Flattens a nested dictionary. | ||
Parameters | ||
---------- | ||
d : MutableMapping | ||
The nested dictionary to flatten. | ||
parent_key : str | ||
The key of the current value being flattened. | ||
sep : str | ||
The separator to be used to join the nested keys. | ||
Returns | ||
------- | ||
MutableMapping | ||
The flattened version of the given nested dictionary. | ||
""" | ||
items = [] | ||
for k, v in dictionary.items(): | ||
new_key = parent_key + sep + k if parent_key else k | ||
if isinstance(v, MutableMapping): | ||
items.extend(flatten_nested_dict(v, new_key, sep=sep).items()) | ||
|
||
else: | ||
items.append((new_key, v)) | ||
|
||
return dict(items) | ||
|
||
|
||
flat_i18n_object_dict = flatten_nested_dict(i18n_object_dict) | ||
|
||
# MARK: Compare Dicts | ||
|
||
assert ( | ||
flat_en_us_json_dict == flat_i18n_object_dict | ||
), "\nThe current i18nMap object doesn't match the base i18n JSON file. Please re-generate the i18nMap object.\n" | ||
|
||
print("\nSuccess: The current i18nMap object matches the base i18n JSON file.\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
""" | ||
Generates a TypeScript file with the object i18nMap that maps all i18n keys as properties. | ||
This allows type checking of all i18n keys to assure that they've been entered correctly. | ||
Usage: | ||
python3 frontend/i18n/check/i18n_generate_map_object.py | ||
""" | ||
|
||
import json | ||
import re | ||
from pathlib import Path | ||
|
||
# MARK: Paths / Files | ||
|
||
i18n_check_dir = Path(__file__).parent.parent.resolve() | ||
|
||
with open(i18n_check_dir / "en-US.json", encoding="utf-8") as f: | ||
en_us_json_dict = json.loads(f.read()) | ||
|
||
# MARK: Create Map | ||
|
||
|
||
def _recursively_nest_dict(k: str, v: str | dict, output_dict: dict): | ||
""" | ||
Recursively nests dictionaries. | ||
Parameters | ||
---------- | ||
k : str | ||
The key of a sub dictionary. | ||
v : str | dict | ||
The value of a nested dictionary. | ||
output_dict | dict | ||
The output_dict dictionary or sub-dictionary. | ||
""" | ||
k, *rest = k.split(".", 1) | ||
if rest: | ||
_recursively_nest_dict(rest[0], v, output_dict.setdefault(k, {})) | ||
|
||
else: | ||
output_dict[k] = v | ||
|
||
|
||
def nest_flat_dict(flat_dict: dict) -> dict: | ||
""" | ||
Nest a flat dictionary. | ||
Parameters | ||
---------- | ||
flat_dict : dict | ||
A flattened dictionary that should be nested. | ||
Returns | ||
------- | ||
nested_dict : dict | ||
The nested version of the original flat dictionary. | ||
""" | ||
nested_dict = {} | ||
for k, v in flat_dict.items(): | ||
_recursively_nest_dict(k=k, v=v, output_dict=nested_dict) | ||
|
||
return nested_dict | ||
|
||
|
||
i18n_map_dict = nest_flat_dict({k: k for k, _ in en_us_json_dict.items()}) | ||
|
||
# MARK: Write Map | ||
|
||
frontend_types_dir = (Path(__file__).parent.parent.parent / "types").resolve() | ||
|
||
with open(frontend_types_dir / "i18n-map.ts", encoding="utf-8", mode="w") as f: | ||
f.write(f"export const i18nMap = {json.dumps(i18n_map_dict, indent=2)}") | ||
|
||
# Rewrite to format the keys to not have quotes. | ||
with open(frontend_types_dir / "i18n-map.ts", encoding="utf-8", mode="r") as f: | ||
i18n_object = f.readlines() | ||
|
||
with open(frontend_types_dir / "i18n-map.ts", encoding="utf-8", mode="w") as f: | ||
for line in i18n_object: | ||
f.write(re.sub(r'"([^"]*)":', r"\1:", line)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.