How to remove a language from String Catalog?
Asked Answered
S

4

11

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

Soonsooner answered 11/9, 2023 at 23:47 Comment(0)
L
5

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)
Laue answered 1/3, 2024 at 19:1 Comment(2)
Thank you very much, this saved me a lot of time.Buttonball
It worked for me too, thanks !Nigritude
S
1

Looks like the only way is to go to the Project settings > Info and find the language. Press it and then hit the - button.

1

Soonsooner answered 11/9, 2023 at 23:51 Comment(2)
This doesn't work for bundles (dynamic frameworks with localisation in)Engross
Don't do this, it doesn't delete the language from the string catalog.Schechinger
S
0

The Xcode team probably forgot that maybe developers need to remove languages so they didn't put a minus button.

Right click on the string catalog and open it as source code.

enter image description here

It is a JSON file that contains all the languages and their state.

Delete the key for the language that you want to remove:

enter image description here

(removing the selected text will remove Hungarian from the string catalog).

Schechinger answered 9/10, 2023 at 11:37 Comment(2)
Nearly impossible to do when you have hundreds of translations. Wish Apple put all the same language strings together in a single block instead of separate languages for each string.Comedown
When doing this it still doesn't remove the language from the catalog listGurkha
P
0

I had to do the same thing and unfortunately the accepted answer didn't work for me. The actual .xcstrings file still contained the translations. What I did was just to use the jq tool that manipulates json in a very easy manner. And the script that did the trick for me was this del(.strings | .[] | .localizations | .en). Just replace the last part with the language you would like to delete.

Pistoleer answered 7/5, 2024 at 14:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.