2016-08-31 44 views
1

我有一個應用程序,我想改變應用程序內的語言,這與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 
} 
} 
+0

所以,你要使用不同的字符串的文件嗎?或者,您使用例如系統按鈕「完成」,並且您想翻譯系統元素? –

+0

我想要使用main.strings(storyboard)和localized.strings。目前,只有本地化的字符串正在被輸入。主要字符串具有選項卡名稱,以及某些選項卡上的菜單項。這就是爲什麼我需要這兩個。我嘗試將菜單項添加到localize.strings,但這不起作用 –

+0

我建議你以編程方式設置視圖元素的所有標題。一小時後,我會爲你製作樣品。 –

回答

1

用於嘗試此代碼的代碼:

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> 

enter image description here

+0

嗨瓦西里,我得到它的工作,標籤欄項目和所有。感謝您的支持。我會用所有代碼更新以前的帖子,以防你想看看我做了什麼。謝謝(你能給我三個問題的投票嗎?) –

+0

我投了票。歡迎您) –

+0

任何用戶看到這篇文章。請到這裏查看所有細節和最終代碼http://stackoverflow.com/questions/39201644/switching-the-language-directly-in-the-application –

相關問題