UIWebview load different files switch langage

假如想象 提交于 2020-01-02 20:45:35

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12917543/uiwebview-load-different-files-switch-langage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!