How to change text encoding of Localizable.strings

2020-02-12 01:42发布

问题:

I am learning how to localize the strings in my project and I am using Xcode 4.

I have generated the base Localizable.strings file, and I want to import this file, changing its encoding from utf-16 to Unicode utf-16 so that the text in the file is readable within xCode. If I strate import this file, when I select it within xcode, the text shows up as gibberish.

In Xcode 3 when you drag the Localizable.strings into your project, the dialog box which appears gives you the option to change the text encoding, but this is not the case in Xcode 4.

Does anyone know a way around this?

回答1:

Maybe this can help you Objective C/Xcode 4: Encoding Problem with Localizable.strings files

These kind of errors can happen if you copy and paste content within Xcode or from external files in your localization files. The consequence is that the encoding of the file changes to for example Western (Mac OS Roman). The Localizable.strings file should be in UTF-16 though.

Solution

1. Like in the picture below, navigate in Xcode to the Localizable.strings location and open it so that you can see all the languages you are supporting.

  1. Left-click on the language file the causes the build error.

  2. Make sure your Utilities View is showing in Xcode. Activate on the button at mark 1 in Picture below.

  1. In the Utilities View select the File Inspector. (Small Logo that looks like Page)

  2. Under Text Settings change the encoding to UTF-16(Marked as 2 in the picture above). The Drop-Down might be grayed out but you can click on it anyways. Click on "Convert" on the Popup.

That's it your project should now compile again.



回答2:

The "Text Settings" for my Localizable.strings files don't have the option of changing the encoding like XCode 3. However, I found if I simply restart Xcode 4, it reinterprets it correctly (UTF-16).



回答3:

Sometimes Xcode displays the warning, even if the file contents are UTF-16, but the file is somehow interpreted as UTF-8. iconv usually says that it can’t convert the file in this case. Converting the file in Xcode to UTF-8 and then converting it back again to UTF-16, like Edmar suggested, solves this problem. The warnings are gone.

To make sure that nothing got broken during the conversion, recheck the whole strings file.



回答4:

After changing the encoding, and re-executing the genstrings command to regenerate the localize string, things should work.



回答5:

View -> Utilities -> Show File Inspector
Change Text Encoding in the file inspector utility view.