简体   繁体   English

WebView(不是WKWebView)调用本地化的HTML文件

[英]WebView (not WKWebView) calling Localized HTML file

A Mac app requires that a HTML file be called in a WebView (the legacy type, not the newer WKWebView) in a localized form to present the user with some content. Mac应用程序要求以本地化形式在WebView(旧类型,而不是较新的WKWebView)中调用HTML文件,以向用户显示一些内容。

As I side note, I realize that WebView should not be used today, and WKWebView is preferred, however this is a legacy app that currently needs support. 正如我要指出的那样,我意识到今天不应该使用WebView,而是首选WKWebView,但这是当前需要支持的旧版应用程序。

I've used a similar method for the iOS version, however it does not seem to be working. 我对iOS版本使用了类似的方法,但是它似乎不起作用。 The HTML files are simply called "Term.HTML" and are placed in each localization folder alongside the localized string and all other localized content. HTML文件简称为“ Term.HTML”,并与本地化字符串和所有其他本地化内容一起放在每个本地化文件夹中。 This is the code I tried to use: 这是我尝试使用的代码:

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:NSLocalizedString(@"fileTerm", nil) ofType:@"html"];
htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];

[termsView takeStringURLFrom:htmlString];

Where my localized strings file each contain a line that says: 我的本地化字符串文件每个都包含一行内容:

"fileTerm" = "Term";

This is what links the declaration of the first line to the actual file. 这就是将第一行的声明链接到实际文件的原因。 It works in iOS. 它适用于iOS。 However, when running the app and the view containing the WebView attempt to the run, XCode will automatically create a breakpoint on the third line when I actually attempt to give the HTML file to "termsView" which is my WebView. 但是,当运行应用程序并尝试运行包含WebView的视图时,当我实际上尝试将HTML文件提供给我的WebView的“ termsView”时,XCode会在第三行自动创建一个断点。 After skipping this breakpoint, and forcing the app to run, the whole view containing the WebView will simply not appear. 跳过此断点并强制应用程序运行之后,包含WebView的整个视图将不会出现。 I would be thankful if anyone knew why this was or if there was a better way to do this? 如果有人知道为什么会这样,或者有更好的方法可以做到这一点,我将不胜感激。 Thank you everyone! 谢谢大家!

may be someone needs in SWift : I solved this problem with saving 3 html file for every language, and then in ViewController class checked current app language. SWift中可能是需要的 :我为每种语言保存了3个html文件,然后在ViewController类中检查了当前应用程序的语言,从而解决了此问题。 And called up the html file for current language 并调出当前语言的html文件

func loadHtmlFile() {
    let preferredLanguage = NSLocale.preferredLanguages[0]
    if preferredLanguage == "kz" {
        let url = Bundle.main.url(forResource: "aboutUs_kz", withExtension:"html")
        let request = URLRequest(url: url!)
        webView.load(request)
    }
    if preferredLanguage == "ru" {
        let url = Bundle.main.url(forResource: "aboutUs_ru", withExtension:"html")
        let request = URLRequest(url: url!)
        webView.load(request)
    }
    if preferredLanguage == "en" {
        let url = Bundle.main.url(forResource: "aboutUs_en", withExtension:"html")
        let request = URLRequest(url: url!)
        webView.load(request)
    }
}

in viewDidLoad() 在viewDidLoad()中

override func viewDidLoad() {
    super.viewDidLoad()
        loadHtmlFile()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM