Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marekswiecznik/3305e7ec2f32c6c2f0f9d2b47fc6ec25 to your computer and use it in GitHub Desktop.
Save marekswiecznik/3305e7ec2f32c6c2f0f9d2b47fc6ec25 to your computer and use it in GitHub Desktop.
Migrate NSLocalizedString occurences in Swift code to SwiftGen
#!/usr/bin/env python3
import os
import re
import string
def to_camel_case(snake_str):
components = snake_str.split('_')
return components[0] + ''.join(string.capwords(x) for x in components[1:])
def convert_case(match_obj):
if match_obj.group(1) is not None:
return 'L10n.' + to_camel_case(match_obj.group(1))
def migrate_localizable_strings_to_swiftgen(path):
with open(path) as f:
content = f.read()
newText = re.sub(r"NSLocalizedString\(\"([^\"]+)\"(, comment: \"[^\"]*\")?\)", convert_case, content)
newText = re.sub(r"String\(format: (L10n\.[a-zA-Z0-9]+), ([^\)]+)\)", "\g<1>(\g<2>)", newText)
with open(path, "w") as f:
f.write(newText)
for root, dirs, files in os.walk("."):
for file in files:
if file.endswith(".swift"):
migrate_localizable_strings_to_swiftgen(os.path.join(root, file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment