UIWebview load different files switch langage

2019-06-02 07:02发布

I have differents embedded HTML files in different languages.
I'd like to load the correct files in a webview based on the current langage.
Is there a way to do that as easily as NSLocalizableString?

thanks

2条回答
三岁会撩人
2楼-- · 2019-06-02 07:17

If for current language you mean the phone language you could achieve your goal doing this :

NSString *locale = [[NSLocale preferredLanguages] objectAtIndex:0];

the method return a two letter string representing the current language, IT(Italian),EN(English),DE(German) etc. So based on the language you should be able to load the corresponding HTML file.

查看更多
Fickle 薄情
3楼-- · 2019-06-02 07:31

If you use "localized resources", for example

  • english version of "index.html" in "en.lproj/index.html",
  • french version of "index.html" in "fr.lproj/index.html",

then many resource functions will automatically select the correct version (based on the user's language setting), e.g.:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];

To create a localized resource "index.html" in Xcode:

  • Add the required languages in the "Localizations" section of the "Info" tab of your project (if you have not done that already).
  • Select "index.html", and click on "Make Localized" in the File Inspector.
  • Select the required languages in the File Inspector.

Now you have one file for each language, for example "index.html (English)" and "index.html (French)", which are copied to the appropriate folder when you install the app.

查看更多
登录 后发表回答