Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift 5应用程序中使用服务器本地化_Ios_Swift_Swiftui_Localization_Swift5 - Fatal编程技术网

Ios 在Swift 5应用程序中使用服务器本地化

Ios 在Swift 5应用程序中使用服务器本地化,ios,swift,swiftui,localization,swift5,Ios,Swift,Swiftui,Localization,Swift5,出于某些原因,我已经在本机ios应用程序中实现了webview,我希望避免更新应用程序的次数超过必要的次数。因此,我不想使用本地本地化,而是从网页获取翻译文件。我在webview和应用程序之间建立了桥梁,并将一个json成功转换为[String:String]以在应用程序中使用 关键是用户可以更改语言,这将向应用程序发送新词典。因此,我使用[String:String]字典作为环境元素 我在以非侵入式方式设置时遇到问题,这意味着我希望代码在视图中尽可能不可见。我只是在学习SwiftUI,从Swi

出于某些原因,我已经在本机ios应用程序中实现了webview,我希望避免更新应用程序的次数超过必要的次数。因此,我不想使用本地本地化,而是从网页获取翻译文件。我在webview和应用程序之间建立了桥梁,并将一个json成功转换为[String:String]以在应用程序中使用

关键是用户可以更改语言,这将向应用程序发送新词典。因此,我使用[String:String]字典作为环境元素

我在以非侵入式方式设置时遇到问题,这意味着我希望代码在视图中尽可能不可见。我只是在学习SwiftUI,从Swift3开始,已经读了两天了


  • 除了使用捆绑包之外,我还监督过这方面的本地本地化实现吗

  • 我不想创建新的自定义视图,比如LocalizedText,而不仅仅是文本

  • 我正在使用一个键在[String:String]字典中进行查找

  • 我希望本地化的实现在所有视图中尽可能不可见


  • 。。。可以,但我更喜欢:

       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)
        }
    }