Search code examples
iosxcodelocalizationstring-catalog

How to remove a language from String Catalog?


I have accidentally added a new language in String Catalog by pressing a + button at the bottom.

However, there is no - button to remove it(like in Targets for example) and also there is nothing in attributes inspector.

If I select it and press back button on keyboard, it will not show a delete pop-up as it usually does in Xcode. Is there any way to delete it?

1


Solution

  • I had this problem today, so I made a python script to modify the file. If you don't use python on your computer, you can use CodeSpaces to setup an area to run the code below.


    • Open your catalog file as source code
    • Simply Copy & Paste the JSON from your Localizable.xcstrings file to an input file (ie local_in.txt)
    • Copy and paste the code below into a python file (ie delocalize.py)
    • Make sure the .txt and .py files are in the same directory
    • Then run the .py file (ie: python delocalize.py)
    • You'll get an output file (ie: local_out.txt) that you can paste back to your Localizable.xcstrings file using FileManager and TextEdit
    import json
    
    def remove_localizations(input_filepath, output_filepath, languages_to_remove):
    
        with open(input_filepath, 'r', encoding='utf-8') as file:      # Load the JSON data from the file
            data = json.load(file)
    
        for string_key in list(data["strings"].keys()):                 # Remove specified languages from the 'strings' section
            string_data = data["strings"][string_key]
            if "localizations" in string_data:                          # Check if 'localizations' exists before trying to access it
                for lang in languages_to_remove:
                    if lang in string_data["localizations"]:
                        del string_data["localizations"][lang]
    
        with open(output_filepath, 'w', encoding='utf-8') as file:     # Write the updated JSON data back to a file
            json.dump(data, file, indent=2, ensure_ascii=False)
    
    
    input_filepath      = 'local_in.txt'        # Name of file with all languages       (ie: 'local_in.txt' file in the same directory, so use the relative path)
    output_filepath     = 'local_out.txt'       # Name of file with the the new changes (ie: 'local_out.txt' the original or you can overwrite the file if you want)
    languages_to_remove = ['de', 'fr']          # Languages to remove                   (in my case, I only wanted to keep the root ('en') language and 'jp')
    
    # Call the function to remove the languages
    remove_localizations(input_filepath, output_filepath, languages_to_remove)