Created
October 6, 2022 10:37
-
-
Save marekswiecznik/3305e7ec2f32c6c2f0f9d2b47fc6ec25 to your computer and use it in GitHub Desktop.
Migrate NSLocalizedString occurences in Swift code to SwiftGen
This file contains 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
#!/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