When exporting XLIFF from Xcode, how to exclude dummy strings?
Asked Answered
M

3

7

I'm using the Xcode's Editor > Export For Localization... to export XLIFF file for translation but the translations for the Main.storyboard includes a lot of unnecessary strings, mostly placeholders/dummies that are useful at design time.

How do I exclude such strings from XLIFF file?

Misstate answered 29/4, 2015 at 14:36 Comment(2)
It should be supported in Xcode. Criminal missing feature.Parody
The XLIFF workflow was introduced in Xcode 6 and excluding views from the localization would be one checkbox in the UI, this is dumb.Silveira
M
3

I've written a script that excludes certain translation.

How it works?
cmd-line: python strip_loc.py input.xliff output.xliff exclude_list.txt [-v]

Example usage:
python strip_loc.py en.xliff en-stripped.xliff exclude_words.txt -v

The exclude_list.txt is a file with a string per line. The script parses this list and creates a dictionary of banned words. If a translation with source containing one of these strings is encountered, the whole translation unit is removed from the output xml/xliff.

Misstate answered 3/5, 2015 at 8:12 Comment(1)
Is it possible to automate this ? For example when exporting the base language from Xcode.Cupulate
T
1

Here is the solution that works with latest python version:

def log(string_to_log):
if args.verbose:
    print(string_to_log)
import argparse
parser = argparse.ArgumentParser(description="Process xliff file against banned words and output new xliff with stripped translation.", epilog="Example usage: strip_loc.py en.xliff en-stripped.xliff exclude_words.txt -v")
parser.add_argument('source', help="Input .xliff file containing all the strings")
parser.add_argument('output', help="Output .xliff file which will containt the stripped strings according to the exclude_list")
parser.add_argument('exclude_list', help="Multi-line text file where every line is a banned string")
parser.add_argument('-v', '--verbose', action="store_true", help="print script steps while working")
args = parser.parse_args()
banned_words = [line.strip().lower() for line in open(args.exclude_list, 'r')]
log("original file: " + args.source)
log("output file: " + args.output)
log("banned words: " + ", ".join(banned_words))
log("")
import xml.etree.ElementTree as ET
ET.register_namespace('',"urn:oasis:names:tc:xliff:document:1.2")
ns = {"n": "urn:oasis:names:tc:xliff:document:1.2"}
with open(args.source, 'r') as xml_file:
    tree = ET.parse(xml_file)
root = tree.getroot()
counter = 1
for file_body in root.findall("./*/n:body", ns):
    for trans_unit in file_body.findall("n:trans-unit", ns):
        source = trans_unit.find("n:source", ns)
        if source.text is not None:
            source = source.text.encode("utf-8").lower()
            source = source.decode("utf-8")
            source = source.strip()
            for banned_word in banned_words:
                if source.find(banned_word) != -1:
                    log(str(counter) + ": removing <trans-unit id=\"" + trans_unit.attrib['id'] + "\">, banned: \"" + banned_word + "\"")
                    file_body.remove(trans_unit)
                    break
                    counter += 1
tree.write(args.output, "utf-8", True)
log("")
print("DONE")

And the usage is the same:

python strip_loc.py en.xliff en-stripped.xliff exclude_words.txt -v

Theorbo answered 13/5, 2022 at 7:43 Comment(0)
R
0

For me I use this XLIFF Online Editor to edit the xliff file. It will be easy to you to ignore the dummy text or anything you need.

Rheumatoid answered 29/4, 2015 at 14:41 Comment(4)
I just tried the online editor, but it helps you translate already exported keys. The XLIFF file will be sent to translators and they do not know which strings are dummy and which not. It's best if such strings are stripped altogether.Misstate
Maybe you can use the online editor and put a keyword in translation of dummy text like "DUMMY" and export it and send to translators. Translators will ignore them. I afraid that Xcode faces issues while importing if you exclude them totally from the XLIFF fileRheumatoid
I just tested what happens when you remove certain strings from the xliff file. When you import back, Xcode gives you warning that some strings are missing and then it uses the source strings but in all caps to notify you that the strings is without translation. So, for dummy text it's not a problem at all to strip the xliff of these.Misstate
The all caps thing is a scheme setting, I believe.Pilcher

© 2022 - 2024 — McMap. All rights reserved.