在故事板中为视图元素运行应用程序切换语言

我有一个应用程序,我想在应用程序内更改语言,这与localize.strings工作正常,但此代码不会触发main.strings文件。

以下是settingsLanguageVC中使用的代码

import UIKit let AppLanguageKey = "AppLanguage" let AppLanguageDefaultValue = "en" var appLanguage: String { get { if let language = NSUserDefaults.standardUserDefaults().stringForKey(AppLanguageKey) { return language } else { NSUserDefaults.standardUserDefaults().setValue(AppLanguageDefaultValue, forKey: AppLanguageKey) return AppLanguageDefaultValue } } set(value) { NSUserDefaults.standardUserDefaults().setValue((value), forKey: AppLanguageKey) } } class ViewController: UIViewController { 

这里是stringsExtenstion.swift中使用的代码

 import Foundation extension String { var localizeString: String { return localizeString(appLanguage) } var localizeStringUsingSystemLang: String { return NSLocalizedString(self, comment: "") } func localizeString(lang:String?) -> String { if let lang = lang { if let path = NSBundle.mainBundle().pathForResource(lang, ofType: "lproj") { let bundle = NSBundle(path: path) return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "") } } return localizeStringUsingSystemLang } } 

试试这个代码:

TabBarController.swift

 import UIKit class TabBarController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. setTabViewControllerParams(0, tabBarItemTitle: "ONE".localizeString, navigationItemTitle: "ONE".localizeString) setTabViewControllerParams(1, tabBarItemTitle: "TWO".localizeString, navigationItemTitle: "TWO".localizeString) } func setTabViewControllerParams(index: Int, tabBarItemTitle: String, navigationItemTitle: String) { if let tabBarItems = tabBar.items { if index < tabBarItems.count { tabBarItems[index].title = tabBarItemTitle } } if let viewControllers = viewControllers { if index < viewControllers.count { if let navigationController = viewControllers[index] as? UINavigationController { if navigationController.viewControllers.count > 0 { let viewController = navigationController.viewControllers[0] viewController.navigationItem.title = navigationItemTitle } } } } } } 

Localizable.strings(俄语)

 "ONE" = "один"; "TWO" = "два"; 

Localizable.strings(英文)

 "ONE" = "one"; "TWO" = "two"; 

Localizable.strings(法语)

 "ONE" = "un"; "DEUX" = "deux"; 

Main.storyboard

 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="nUR-Hp-pCa"> <dependencies> <deployment identifier="iOS"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/> </dependencies> <scenes> <!--View Controller--> <scene sceneID="CyN-qL-pwy"> <objects> <viewController id="pYF-J2-lCH" sceneMemberID="viewController"> <layoutGuides> <viewControllerLayoutGuide type="top" id="7sj-ZO-H6Z"/> <viewControllerLayoutGuide type="bottom" id="z8T-yS-yZZ"/> </layoutGuides> <view key="view" contentMode="scaleToFill" id="fUH-or-StF"> <rect key="frame" x="0.0" y="0.0" width="600" height="600"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> </view> <navigationItem key="navigationItem" id="t7h-zd-rWW"/> </viewController> <placeholder placeholderIdentifier="IBFirstResponder" id="yti-8g-g3h" userLabel="First Responder" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="1837" y="905"/> </scene> <!--View Controller--> <scene sceneID="qSl-mU-Ice"> <objects> <viewController id="6og-vK-uE7" sceneMemberID="viewController"> <layoutGuides> <viewControllerLayoutGuide type="top" id="8pT-ac-TqX"/> <viewControllerLayoutGuide type="bottom" id="xne-JP-NAW"/> </layoutGuides> <view key="view" contentMode="scaleToFill" id="cy0-Zm-dW4"> <rect key="frame" x="0.0" y="0.0" width="600" height="600"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> </view> <navigationItem key="navigationItem" id="Yme-f2-Ybt"/> </viewController> <placeholder placeholderIdentifier="IBFirstResponder" id="sj3-Kd-b6r" userLabel="First Responder" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="1837" y="244"/> </scene> <!--Tab Bar Controller--> <scene sceneID="hYk-it-5Yg"> <objects> <tabBarController id="nUR-Hp-pCa" customClass="TabBarController" customModule="stackoverflow_39242174" customModuleProvider="target" sceneMemberID="viewController"> <tabBar key="tabBar" contentMode="scaleToFill" id="ZIu-fS-Ven"> <rect key="frame" x="0.0" y="0.0" width="320" height="49"/> <autoresizingMask key="autoresizingMask"/> <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/> </tabBar> <connections> <segue destination="9ZZ-tm-gKt" kind="relationship" relationship="viewControllers" id="tba-rV-XaM"/> <segue destination="QV0-6J-vKg" kind="relationship" relationship="viewControllers" id="MnC-tA-o6W"/> </connections> </tabBarController> <placeholder placeholderIdentifier="IBFirstResponder" id="5R8-Ki-Qv1" userLabel="First Responder" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="205" y="575"/> </scene> <!--Item 1--> <scene sceneID="5eX-TM-S1f"> <objects> <navigationController automaticallyAdjustsScrollViewInsets="NO" id="9ZZ-tm-gKt" sceneMemberID="viewController"> <tabBarItem key="tabBarItem" title="Item 1" id="6fX-a0-rTY"/> <toolbarItems/> <navigationBar key="navigationBar" contentMode="scaleToFill" id="LaV-6J-YLJ"> <rect key="frame" x="0.0" y="0.0" width="320" height="44"/> <autoresizingMask key="autoresizingMask"/> </navigationBar> <nil name="viewControllers"/> <connections> <segue destination="6og-vK-uE7" kind="relationship" relationship="rootViewController" id="H4K-P1-t3E"/> </connections> </navigationController> <placeholder placeholderIdentifier="IBFirstResponder" id="n57-4l-GAt" userLabel="First Responder" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="1025" y="244"/> </scene> <!--Item 2--> <scene sceneID="nqO-69-Jkp"> <objects> <navigationController automaticallyAdjustsScrollViewInsets="NO" id="QV0-6J-vKg" sceneMemberID="viewController"> <tabBarItem key="tabBarItem" title="Item 2" id="YyT-40-7qX"/> <toolbarItems/> <navigationBar key="navigationBar" contentMode="scaleToFill" id="ukb-bm-c5h"> <rect key="frame" x="0.0" y="0.0" width="320" height="44"/> <autoresizingMask key="autoresizingMask"/> </navigationBar> <nil name="viewControllers"/> <connections> <segue destination="pYF-J2-lCH" kind="relationship" relationship="rootViewController" id="gRY-up-TJA"/> </connections> </navigationController> <placeholder placeholderIdentifier="IBFirstResponder" id="geq-VQ-SVO" userLabel="First Responder" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="1025" y="905"/> </scene> </scenes> </document> 

在这里输入图像说明