Skip to content

Commit

Permalink
Add 'obsolete' comments in translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Uwuewsky committed Nov 3, 2024
1 parent 9f98a02 commit a7cb64c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
15 changes: 15 additions & 0 deletions doc/OBSOLETION_AND_MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,18 @@ For mods, you need to add an `"obsolete": true,` boolean into MOD_INFO, which pr
"obsolete": true
}
```

When declaring a mod obsolete, also consider adding its directory to the `lang/update_pot.sh` file via the `-D` argument to `extract_json_strings.py`:

```diff
echo "> Extracting strings from JSON"
if ! lang/extract_json_strings.py \
-i data \
...
-D data/mods/BlazeIndustries \
-D data/mods/desert_region \
+ -D data/mods/YOUR_DEPRECATED_MOD \
-n "$package $version" \
-r lang/po/gui.pot \
-o lang/po/json.pot
```
10 changes: 9 additions & 1 deletion lang/extract_json_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
parser.add_option("-x", "--exclude_dir", dest="exclude_dir",
action="append", type="str",
help="exclude directories")
parser.add_option("-D", "--obsolete_paths", dest="obsolete_paths",
action="append", type="str",
help="obsolete directories or files")

(options, args) = parser.parse_args()

Expand All @@ -46,6 +49,9 @@
exclude_dir = [os.path.normpath(i) for i in options.exclude_dir] \
if options.exclude_dir else []

obsolete_paths = [os.path.normpath(i) for i in options.obsolete_paths] \
if options.obsolete_paths else []


def extract_all_from_dir(json_dir):
"""Extract strings from every json file in the specified directory,
Expand All @@ -72,7 +78,9 @@ def main():
extract_all_from_dir(i)

with open(options.output, mode="w", encoding="utf-8") as fp:
write_to_pot(fp, True, options.name, sanitize=options.reference)
write_to_pot(fp, True, options.name,
sanitize=options.reference,
obsolete_paths=obsolete_paths)


main()
39 changes: 34 additions & 5 deletions lang/string_extractor/pot_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,42 @@
from .message import messages, occurrences


def deduplciate(comments):
"""Remove duplicate comment lines while preserving order."""
seen = set()
def process_comments(comments, origins, obsolete_paths):
result = []

# remove duplicate comment lines while preserving order
seen = set()
for comment in comments:
if comment not in seen:
result.append(comment)
seen.add(comment)

# add 'obsolete' comment if this string
# has only obsolete files in the origins
obsolete_count = 0
for origin in origins:
is_obsolete = False

if "obsolet" in origin:
# if the file path contains "obsolete"/"obsoletion"
is_obsolete = True
else:
# if the file path matches the obsolete paths
# explicitly specified in the '-D' arguments
for o_path in obsolete_paths:
p = os.path.commonpath([o_path, origin])
if p in obsolete_paths:
is_obsolete = True
break
if is_obsolete:
obsolete_count += 1

if obsolete_count == len(origins):
return [
"[DEPRECATED] Don't translate this line.",
"This string is from an obsolete source "
"and will no longer be used in the game."
]
return result


Expand Down Expand Up @@ -71,7 +99,8 @@ def sanitize_plural_colissions(reference):
messages[pair][0].text_plural = entry.msgid_plural


def write_to_pot(fp, with_header=True, pkg_name=None, sanitize=None):
def write_to_pot(fp, with_header=True, pkg_name=None,
sanitize=None, obsolete_paths=[]):
if sanitize:
sanitize_plural_colissions(sanitize)
if with_header:
Expand All @@ -93,7 +122,7 @@ def write_to_pot(fp, with_header=True, pkg_name=None, sanitize=None):
origin = " ".join(sorted(origins))

# translator comments
for line in deduplciate(comments):
for line in process_comments(comments, origins, obsolete_paths):
print("#. ~ {}".format(line), file=fp)

# reference
Expand Down
2 changes: 2 additions & 0 deletions lang/update_pot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ if ! lang/extract_json_strings.py \
-X data/json/npcs/TALK_TEST.json \
-X data/core/sentinels.json \
-X data/raw/color_templates/no_bright_background.json \
-D data/mods/BlazeIndustries \
-D data/mods/desert_region \
-n "$package $version" \
-r lang/po/gui.pot \
-o lang/po/json.pot
Expand Down

0 comments on commit a7cb64c

Please sign in to comment.