Skip to content

Commit 25426cf

Browse files
committed
When Twine generates files it ignores empty folders for langs
1 parent 592fd05 commit 25426cf

File tree

1 file changed

+26
-47
lines changed

1 file changed

+26
-47
lines changed

python_twine/twine/runner.py

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,13 @@ def generate_all_localization_files(self):
9696
"""Generate localization files for all languages."""
9797
if self.options.get("validate"):
9898
self.validate_twine_file()
99+
create_folders = self.options.get("create_folders")
99100

100101
output_path = Path(self.options["output_path"])
101102

102103
# Create output directory if needed
103104
if not output_path.is_dir():
104-
if self.options.get("create_folders"):
105+
if create_folders:
105106
output_path.mkdir(parents=True, exist_ok=True)
106107
else:
107108
raise TwineError(f"Directory does not exist: {output_path}")
@@ -112,59 +113,37 @@ def generate_all_localization_files(self):
112113
file_name = self.options.get("file_name") or formatter.default_file_name()
113114
encoding = self.options.get("encoding", "UTF-8")
114115

115-
if self.options.get("create_folders"):
116-
# Create folders for all languages
117-
for lang in self.twine_file.language_codes:
118-
lang_path = output_path / formatter.output_path_for_language(lang)
119-
lang_path.mkdir(parents=True, exist_ok=True)
116+
language_found = False
120117

121-
file_path = lang_path / file_name
122-
output = formatter.format_file(lang)
118+
# Create folders for all languages
119+
for lang in self.twine_file.language_codes:
120+
lang_path = output_path / formatter.output_path_for_language(lang)
121+
lang_path.mkdir(parents=True, exist_ok=True)
123122

124-
if not output:
125-
print(
126-
f"Skipping {file_path} since it would not contain any translations.",
127-
file=twine.stdout,
128-
)
129-
continue
130-
131-
with open(file_path, "w", encoding=encoding) as f:
132-
f.write(output)
133-
134-
print(f"Generated {file_path}", file=twine.stdout)
135-
else:
136-
# Find existing language directories
137-
language_found = False
138-
139-
for item in output_path.iterdir():
140-
if not item.is_dir():
141-
continue
142-
143-
lang = formatter.determine_language_given_path(str(item))
144-
if not lang:
145-
continue
146-
147-
language_found = True
123+
file_path = lang_path / file_name
124+
if not file_path.exists() and not create_folders:
125+
# Don't create file for 'lang'. Only update existing files.
126+
continue
148127

149-
file_path = item / file_name
150-
output = formatter.format_file(lang)
128+
output = formatter.format_file(lang)
129+
language_found = True
151130

152-
if not output:
153-
print(
154-
f"Skipping {file_path} since it would not contain any translations.",
155-
file=twine.stdout,
156-
)
157-
continue
131+
if not output:
132+
print(
133+
f"Skipping {file_path} since it would not contain any translations.",
134+
file=twine.stdout,
135+
)
136+
continue
158137

159-
with open(file_path, "w", encoding=encoding) as f:
160-
f.write(output)
138+
with open(file_path, "w", encoding=encoding) as f:
139+
f.write(output)
161140

162-
print(f"Generated {file_path}", file=twine.stdout)
141+
print(f"Generated {file_path}", file=twine.stdout)
163142

164-
if not language_found:
165-
raise TwineError(
166-
f"Failed to generate any files: No languages found at {output_path}"
167-
)
143+
if not language_found:
144+
raise TwineError(
145+
f"Failed to generate any files: No languages found at {output_path}"
146+
)
168147

169148
def consume_localization_file(self):
170149
"""Import translations from a localization file."""

0 commit comments

Comments
 (0)