Batch Convert XCode String files, from utf-16 to utf-8
SITUATION
Old Version Xcode create utf-16 string file. It is not easy to diff changes.
TASK
Batch convert string files from utf-16 to utf-8.
ACTION
step 0: change one string file to utf-8
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 Trueexcept Exception as e:
# ori file maybe not from_encoding.
os.remove(utf8_tmp_path)
return False
step 1: batch convert
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")
step 2: maybe need to change xcode editor’s config, if build failed.

Result
Easy to review diff in xcode string files.