Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement NSLocalizedString #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RieLCho
Copy link

@RieLCho RieLCho commented Sep 15, 2024

스크린샷 2024-09-15 오후 9 53 37

So I tried to implement NSLocalizedString, which I issued earlier (MaaAssistantArknights/MaaAssistantArknights#8770)

but I think I failed to collect all of the hardcoded chinese.

python

import os
import re

def find_and_replace_hardcoded_strings(directory, localizable_file, log_file):
    # 정규 표현식을 수정하여 하드코딩된 문자열만을 정확히 찾아냅니다.
    pattern = re.compile(r'(?<!NSLocalizedString\()\"([^\"]+?)\"(?!\s*comment:)')
    hanzi_pattern = re.compile(r'[\u4e00-\u9fff]')
    localizable_strings = {}
    changes_log = []
    exclude_strings = [')', '(', '+', ':', ',']

    # Step 1: Find hardcoded strings and add to Localizable.strings
    for root, _, files in os.walk(directory):
        for file in files:
            if file.endswith(".swift"):
                file_path = os.path.join(root, file)
                with open(file_path, 'r', encoding='utf-8') as f:
                    content = f.read()
                    matches = pattern.findall(content)
                    for match in matches:
                        if match not in localizable_strings and hanzi_pattern.search(match):
                            localizable_strings[match] = match

    # Write to Localizable.strings file
    with open(localizable_file, 'w', encoding='utf-8') as f:
        for key, value in localizable_strings.items():
            f.write(f'"{key}" = "{value}";\n')

    # Step 2: Replace hardcoded strings with NSLocalizedString
    for root, _, files in os.walk(directory):
        for file in files:
            if file.endswith(".swift"):
                file_path = os.path.join(root, file)
                with open(file_path, 'r', encoding='utf-8') as f:
                    content = f.read()
                    new_content = content
                    for match in pattern.findall(content):
                        if '\n' not in match and not match.startswith('NSLocalizedString') and not any(exclude in match for exclude in exclude_strings) and hanzi_pattern.search(match):  # 한 줄에 있는 문자열만 매칭
                            new_content = new_content.replace(f'"{match}"', f'NSLocalizedString("{match}", comment: "")')
                            changes_log.append(f'"{match}"="{match}"\n')
                with open(file_path, 'w', encoding='utf-8') as f:
                    f.write(new_content)

    # Write changes log to file
    with open(log_file, 'w', encoding='utf-8') as f:
        for log in changes_log:
            f.write(log)

if __name__ == "__main__":
    find_and_replace_hardcoded_strings(".", "/Resources/zh-Hans.lproj/Localizable.strings", "/Resources/zh-Hans.lproj/Localizable.strings")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant