私は既にクラスを持っています。言語マネージャは私のために文字列をローカライズしています。これは、最も重要な方法は、私はそれが行くようにそれらをローカライズ、ビューコントローラ内のすべてのビューを再帰的にループする方法を書くことで、この時に拡張され、この
func localizeString(stringToLocalize:String) -> String
{
// Get the corresponding bundle path.
let selectedLanguage = self.getLanguage()
let path = Bundle.main.path(forResource: selectedLanguage, ofType: "lproj")
// Get the corresponding localized string.
let languageBundle = Bundle(path: path!)
return languageBundle!.localizedString(forKey: stringToLocalize, value: "", table: nil)
}
で非常に単純なクラスです。私はこれを共有することにしました。これは非常に便利だと思っています。どのView Controllerでもプラグアンドプレイとして動作し、Storyboardyストリングファイルをエクスポートして追加し、変更があったときはいつでも再統合するサイクルを避けます。これにより、Localizable.stringsファイルに追加するだけで、すべてが自動的に処理されます。
func localizeUI(parentView:UIView)
{
for view:UIView in parentView.subviews
{
if let potentialButton = view as? UIButton
{
if let titleString = potentialButton.titleLabel?.text {
potentialButton.setTitle(localizeString(stringToLocalize: titleString), for: .normal)
}
}
else if let potentialLabel = view as? UILabel
{
if potentialLabel.text != nil {
potentialLabel.text = localizeString(stringToLocalize: potentialLabel.text!)
}
}
localizeUI(parentView: view)
}
}