Created
March 16, 2022 04:42
-
-
Save yanfeng42/a8c69a75a58cb1a1952d9e515ed4f856 to your computer and use it in GitHub Desktop.
Batch Convert XCode String files, from utf-16 to utf-8
This file contains hidden or 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
import codecs | |
import os | |
import shutil | |
import glob | |
def convert(file_path: str, from_encoding: str, to_encoding: str): | |
utf16_path = file_path | |
utf8_tmp_path = utf16_path + ".tmp" | |
try: | |
with codecs.open(utf16_path, | |
encoding=from_encoding) as input_file: | |
with codecs.open( | |
utf8_tmp_path, "w", | |
encoding=to_encoding) as output_file: | |
shutil.copyfileobj(input_file, output_file) | |
shutil.move(utf8_tmp_path, utf16_path) | |
return True | |
except Exception as e: | |
# ori file maybe not from_encoding. | |
os.remove(utf8_tmp_path) | |
return False | |
def batch_convert(dir_path: str): | |
os.chdir(dir_path) | |
for file in glob.glob("**/*.strings", recursive=True): | |
file_full_path = os.path.join(dir_path, file) | |
convert(file_path=file_full_path, from_encoding="utf-16", to_encoding="utf-8") | |
if __name__ == '__main__': | |
some_dir_path = $path_to_i18n | |
batch_convert(dir_path=some_dir_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment