stackich
stackich

Reputation: 5207

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

Upvotes: 12

Views: 2350

Answers (5)

laike9m
laike9m

Reputation: 19308

Based on Blaine's answer, I tweaked the code a little bit to specify localizations to keep (instead of removal). Tested and works perfectly for me.

import json


def remove_localizations(input_filepath, output_filepath, languages_to_keep):
    with open(input_filepath, "r", encoding="utf-8") as file:
        data = json.load(file)

    for string_key in list(data["strings"].keys()):
        string_data = data["strings"][string_key]
        if "localizations" not in string_data:
            continue

        print("Found localization")
        languages = [lang for lang in string_data["localizations"]]
        print(languages)
        for lang in languages:
            if lang not in languages_to_keep:
                del string_data["localizations"][lang]

    with open(output_filepath, "w", encoding="utf-8") as file:
        json.dump(data, file, indent=2, ensure_ascii=False)


languages_to_keep = ["en", "fr", "it", "zh-Hans", "zh-Hant"]
remove_localizations("Localizable.xcstrings", "new.xcstrings", languages_to_keep)

Upvotes: 0

Blaine L
Blaine L

Reputation: 86

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)

Upvotes: 7

gasho
gasho

Reputation: 1931

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.

Upvotes: 0

apptimal
apptimal

Reputation: 494

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).

Upvotes: -1

stackich
stackich

Reputation: 5207

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

Upvotes: 2

Related Questions