Ios 在Swift 5应用程序中使用服务器本地化
出于某些原因,我已经在本机ios应用程序中实现了webview,我希望避免更新应用程序的次数超过必要的次数。因此,我不想使用本地本地化,而是从网页获取翻译文件。我在webview和应用程序之间建立了桥梁,并将一个json成功转换为[String:String]以在应用程序中使用 关键是用户可以更改语言,这将向应用程序发送新词典。因此,我使用[String:String]字典作为环境元素 我在以非侵入式方式设置时遇到问题,这意味着我希望代码在视图中尽可能不可见。我只是在学习SwiftUI,从Swift3开始,已经读了两天了Ios 在Swift 5应用程序中使用服务器本地化,ios,swift,swiftui,localization,swift5,Ios,Swift,Swiftui,Localization,Swift5,出于某些原因,我已经在本机ios应用程序中实现了webview,我希望避免更新应用程序的次数超过必要的次数。因此,我不想使用本地本地化,而是从网页获取翻译文件。我在webview和应用程序之间建立了桥梁,并将一个json成功转换为[String:String]以在应用程序中使用 关键是用户可以更改语言,这将向应用程序发送新词典。因此,我使用[String:String]字典作为环境元素 我在以非侵入式方式设置时遇到问题,这意味着我希望代码在视图中尽可能不可见。我只是在学习SwiftUI,从Swi
。。。可以,但我更喜欢:
Text("greetings") // Displays "Hello world!"
// Example of an implementation of the existing localization
struct ContentView: View {
var body: some View {
Text("")
.localized("Greetings") // Outputs "Hello world!"
}
}
// Setting up environment object
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
var dictionary = DictionaryViewModel()
let contentView = ContentView().environmentObject(dictionary)
// No magic here; same as usual
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
/../
}
// Called from the webview via dictionaryViewModel.send([String: String]) after the webpage has loaded.
final class DictionaryViewModel: ObservableObject {
@Published var list = [String: String]() // ex. {"greetings": "Hello world"}
}
// Wraps a Modifier()
extension Text {
func localized(_ key: String) -> some View {
modifier(Localized(key: key))
}
}
struct Localized: ViewModifier {
@EnvironmentObject var dictionary: DictionaryViewModel
let key: String
func body(content: Content) -> some View {
if let translation = dictionary.list[key.lowercased()] {
return Text(translation)
}
return Text(key)
}
}
[编辑]跟进 我在运行这段代码时遇到一些图形错误,某些文本块几乎或完全透明。我发现我需要重新启动XCode及其模拟器,以使bug消失
通过谷歌搜索,我找到了可以使用
manager.createFile
存储字典的地方,然后使用内置bundle参数。不管怎样,我还是选择了我的解决方案,因为它和那个解决方案一样麻烦。这些本地化字符串多久会被更改一次?在云中移动它们会让事情变得更加复杂,必须是每隔几天就会不断变化的特殊文本。该应用程序不会持续工作,否则我会同意你的看法。使用云翻译还有其他好处——非开发人员可以更新语言字符串。在这家公司,这对美国开发人员来说是一件幸事。虽然这本身不是一个完整的解决方案,但我可以推荐attranslate
将JSON文件转换为iOS字符串:
// Example of an implementation of the existing localization
struct ContentView: View {
var body: some View {
Text("")
.localized("Greetings") // Outputs "Hello world!"
}
}
// Setting up environment object
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
var dictionary = DictionaryViewModel()
let contentView = ContentView().environmentObject(dictionary)
// No magic here; same as usual
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
/../
}
// Called from the webview via dictionaryViewModel.send([String: String]) after the webpage has loaded.
final class DictionaryViewModel: ObservableObject {
@Published var list = [String: String]() // ex. {"greetings": "Hello world"}
}
// Wraps a Modifier()
extension Text {
func localized(_ key: String) -> some View {
modifier(Localized(key: key))
}
}
struct Localized: ViewModifier {
@EnvironmentObject var dictionary: DictionaryViewModel
let key: String
func body(content: Content) -> some View {
if let translation = dictionary.list[key.lowercased()] {
return Text(translation)
}
return Text(key)
}
}