Copies a .strings file using iconv to reconvert the input into UTF-16.
(self, source, dest)
| 132 | ) |
| 133 | |
| 134 | def _CopyStringsFile(self, source, dest): |
| 135 | """Copies a .strings file using iconv to reconvert the input into UTF-16.""" |
| 136 | input_code = self._DetectInputEncoding(source) or "UTF-8" |
| 137 | |
| 138 | # Xcode's CpyCopyStringsFile / builtin-copyStrings seems to call |
| 139 | # CFPropertyListCreateFromXMLData() behind the scenes; at least it prints |
| 140 | # CFPropertyListCreateFromXMLData(): Old-style plist parser: missing |
| 141 | # semicolon in dictionary. |
| 142 | # on invalid files. Do the same kind of validation. |
| 143 | import CoreFoundation # noqa: PLC0415 |
| 144 | |
| 145 | with open(source, "rb") as in_file: |
| 146 | s = in_file.read() |
| 147 | d = CoreFoundation.CFDataCreate(None, s, len(s)) |
| 148 | _, error = CoreFoundation.CFPropertyListCreateFromXMLData(None, d, 0, None) |
| 149 | if error: |
| 150 | return |
| 151 | |
| 152 | with open(dest, "wb") as fp: |
| 153 | fp.write(s.decode(input_code).encode("UTF-16")) |
| 154 | |
| 155 | def _DetectInputEncoding(self, file_name): |
| 156 | """Reads the first few bytes from file_name and tries to guess the text |
no test coverage detected