IOS開發入門(9)-本地化(2)

可算是放假了,繼續學習

  上一節講了添加中文。這個遇到的問題比較少。現在我們來添加一下德語(書上的),這裏是有一些問題在添加中文的時候沒有遇到的。

  在上一節中,我們已經將大部分文件都添加完畢了。這裏只需要增加一個德語文件就行了。添加方法跟中文的是一樣的。

  文件內容如下:(畢竟是德語)
  

/* Label for the information of one car */
"CarInfoLabel" = "Information Zum Auto";

/* Make Label for the make of one car */
"CarInfoMakeLabel" = "Marke";

/* Model label for the model of one car */
"CarInfoModelLabel" = "Modell";

/* Year label for one car */
"CarInfoYearLabel" = "Jahr";

/* Label for the total number of current car */
"CarNumberLabel" = "
 des Autos";

/* Title for button to edit the current car */
"EditCarButton" = "Bearbeiten";

/* Button to create and add a new car */
"NewCarButton" = "Neues Auto";

/* Title for button to go to the next car */
"NextCarButton" = "Nåchstes";

/* Title for button to go to the previous car */
"PreviousCarButton" = "Vorheriges";

/* Label for the total number of cars */
"TotalCarsLabel" = "Anzahl aller Autos";

/* Placeholder string for an unknown car make */
"UnknownMakePlaceholder" = "Unbekannte Marke";

/* Placeholder string for an unknown car model */
"UnknownModelPlaceholder" = "Unbekanntes";

/* Label for the line to enter or edit the Make of a car */
"CarMakeFieldLabel" = "Marke";

/* Label for the line to enter or edit the Model of a car */
"CarModelFieldLabel" = "Modell";

/* Label for the line to enter or edit the Year of a car */
"CarYearFieldLabel" = "Jahr";

/* Label for the line to enter or edit the Fuel of a car */
"CarFuelFieldLabel" = "Kraftstoff";

"AddViewScreenTitle" = "AutoParker";

"EditViewScreenTitle" = "Auto Bearbeiten";

"TotalNumber" = "Anzahl aller Autos:";
"CurrentNumber" = "Nummber des Autos:";
"CarInfor" = "Information Zum Auto";

  換成德語之後,在縱向屏幕的情況下,信息頁面與編輯頁面沒有問題,但是,當我們橫屏的時候,會發現信息頁面出現了問題如圖:

這裏寫圖片描述

  翻譯成德語之後的Total Cars標籤太長。核心問題是翻譯的字符串太長了,無法在由添加汽車分組提供的一行空間中顯示。通常情況下,我們看到的的是帶有延續字符的被截斷字符串,類似“Anzahl allere Au…”。

  原因是標籤的寬度沒有被限制爲它的容器寬度。結果邊框延伸,越過容器的後邊緣。我們可能希望標籤的容器視圖去裁剪字符串,也就是切掉標籤在添加汽車分組視圖的邊框之外繪製的任何部分。視圖能夠剪裁它們的內容,但需要在IB或代碼中設置標記。某些類型的視圖元素默認設置了該標籤,但不是UIView。

  嘗試爲添加汽車分組設置Clip Subviews標記

這裏寫圖片描述

選中後,運行程序

這裏寫圖片描述

  標籤會被裁剪,但不太好看,因爲有字母確實。

  這時候我們能想到的還有約束!我們要找到一組約束,用於爲任何方向的任意本地化字符串調整Car Total標籤的大小。可以通過將標籤的行數設置爲0,使其成爲多行標籤來實現這一點。

  好了,開始添加約束

  首先去除Car Total標籤原來的約束:

這裏寫圖片描述

  添加新約束:

這裏寫圖片描述

  標籤與按鈕之間的約束:

  先用線連起來(右鍵拉過去)

這裏寫圖片描述

  修改約束爲≥0,因爲標籤會變大,但是按鈕不變,這是爲了避免按鈕被標籤擋住了。

這裏寫圖片描述

  然後下面是運行結果:

這裏寫圖片描述

  好了,到這爲止,關於換字體的方法已經完啦,接下來還有別的事,我們的本地化還沒有結束!

格式化和讀取數字

  不同時區之間,數字和日期的格式化也個各不相同。在IOS中,語言和區域是分開設置的。

  德語數字中的點號和逗號分隔符是點到的。英語中的數字1,234.56,在德語中是1.234,56。當前,我們的程序使用的是stringWithFormat顯示燃油量,用floatValue讀取。這些方法都不適合用於本地化的數字。要實現本地化,我們需要NSNumberFormatter類,該類可以用於創建和解析本地化的數字,以及完成其他格式化任務。

  創建字符串要麼使用類方法localizedNumberFromString:numberStyle:,要麼使用實例方法stringFromNumber:。讀取字符串需要numberFromString:,它與stringFromNumber:一樣需要多點時間以設置格式轉換。

  要添加本地化的數字顯示和解析,只需要在CarEditViewController.m中添加幾行代碼即可:

  1. 通過將viewDidLoad中設置self.fuelField.text的代碼替換爲如下代,顯示本地化的燃油量:
    self.fuelField.text = [NSString localizedStringWithFormat:@"%0.2f",self.currentCar.fuelAmount];

  2. 在prepareForSegue:sender:中,用以下代碼替換更新self.currentCar.fuelAmount的代碼行:NSNumberFormatter *readFuel = [NSNumberFormatter new];
    readFuel.locale = [NSLocale currentLocale];
    [readFuel setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber *fuelNum = [readFuel numberFromString:self.fuelField.text];
    self.currentCar.fuelAmount = [fuelNum floatValue];

  3. 在viewWillDisapper中執行與步驟2相同的代碼修改任務。

      運行程序,進入編輯頁面,你會發現我們的修改已經生效了!

從右至左:阿拉伯語國際化

  我們創建的幾個約束使用了前後邊緣,而不是左右邊緣。在接下來的內容中會說明原因

  漢語英語德語是從左向右的語言,這適用於一條句子的流動方向和每個單詞的流動方向。將“Mary had a little lamb”修改爲從右向意味着反轉整個句子爲“bmal elttil a dah yraM”,即使這麼修改,也沒有抓住真正的區別,因爲這話中可能有非母語詞彙。如Mary,這個正確的名字,是外國人,那麼從右到左的版本將是“bmal elttil a dah Mary”。

  注意,接下來的內容適用於任何從右到左的語言,包括Hebrew(希伯來文)、N’KO(非洲語)、Thaana(馬爾代夫語)和其他一些語言。

  要了解針對從右到左的所有語言進行國家化的所有問題,最好的辦法就是去實踐。

  添加語言我就不再說了

  阿拉伯語的版本(google大法好)

/* Label for the information of one car */
"CarInfoLabel" = "معلومات السيارة";

/* Make Label for the make of one car */
"CarInfoMakeLabel" = "علامة ";

/* Model label for the model of one car */
"CarInfoModelLabel" = "نموذج";

/* Year label for one car */
"CarInfoYearLabel" = "عام";

/* Label for the total number of current car */
"CarNumberLabel" = "عدد السيارات";

/* Title for button to edit the current car */
"EditCarButton" = "تحرير";

/* Button to create and add a new car */
"NewCarButton" = "سيارة جديدة";

/* Title for button to go to the next car */
"NextCarButton" = "التالي";

/* Title for button to go to the previous car */
"PreviousCarButton" = "سابق";

/* Label for the total number of cars */
"TotalCarsLabel" = "مجموع السيارات";

/* Placeholder string for an unknown car make */
"UnknownMakePlaceholder" = "جعل غير معروف";

/* Placeholder string for an unknown car model */
"UnknownModelPlaceholder" = "نموذج غير معروف";

/* Label for the line to enter or edit the Make of a car */
"CarMakeFieldLabel" = "علامة";

/* Label for the line to enter or edit the Model of a car */
"CarModelFieldLabel" = "نموذج";

/* Label for the line to enter or edit the Year of a car */
"CarYearFieldLabel" = "عام";

/* Label for the line to enter or edit the Fuel of a car */
"CarFuelFieldLabel" = "وقود";

/* Title for the main app screen */
"AddViewScreenTitle" = "خادم سيارة";

"EditViewScreenTitle" = "تحرير سيارة";

"TotalNumber" = "مجموع السيارات";
"CurrentNumber" = "عدد السيارات";
"CarInfor" = "معلومات السيارة";

  運行結果是這個樣子的

這裏寫圖片描述

  好了,現在我們要做的是讓日期和數字能夠正常工作

  上圖看起來基本是正確的,但是,汽車數量和時間沒有被正確地本地化。然而,事實證明它們已經本地化了。

  我們點開編輯,到裏面會發現耗油已經成功本地化了,而年份沒有。耗油本地化的原因是我們之前將stringWithFormat:修改成了localizedStringWithFormat:,我們接下來要做的,就是把汽車數量和時間給修改了:

在Car.m中

- (NSString *)carInfo {
    NSString *infoLabel = NSLocalizedStringWithDefaultValue(@"CarInfoLabel", nil, [NSBundle mainBundle], @"Car Info", @"Label for the information of one car");
    NSString *makeLabel = NSLocalizedStringWithDefaultValue(@"CarInfoMakeLabel", nil, [NSBundle mainBundle], @"Make ", @"Make Label for the make of one car");
    NSString *modelLabel = NSLocalizedStringWithDefaultValue(@"CarInfoModelLabel", nil, [NSBundle mainBundle], @"Model", @"Model label for the model of one car");
    NSString *yearLabel = NSLocalizedStringWithDefaultValue(@"CarInfoYearLabel", nil, [NSBundle mainBundle], @"Year", @"Year label for one car");
    NSString *unknownMake = NSLocalizedStringWithDefaultValue(@"UnknownMakePlaceholder", nil, [NSBundle mainBundle], @"Unknown Make", @"Placeholder string for an unknown car make");

    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];//1
    [dateComponents setYear:self.year];//2
    NSDate *yearDate = [[NSCalendar currentCalendar]dateFromComponents:dateComponents];//3
    NSString *yearFormat = [NSDateFormatter dateFormatFromTemplate:@"YYYY"//4
                                                           options:0
                                                            locale:[NSLocale currentLocale]];
                            NSDateFormatter *yearFormatter = [[NSDateFormatter alloc]init];
    [yearFormatter setDateFormat:yearFormat];//5

    NSString *localYear = [yearFormatter stringFromDate:yearDate];//6

    NSString *unknownModel = NSLocalizedStringWithDefaultValue(@"UnknownModelPlaceholder", nil, [NSBundle mainBundle], @"Unknown Model", @"Placeholder string for an unknown car model");

    return [NSString stringWithFormat:@"%@\n %@: %@\n %@: %@\n %@: %@",//7
            infoLabel,makeLabel,
            self.make ? self.make : unknownMake,
            modelLabel,
            self.model ? self.model : unknownModel,
            yearLabel,localYear];//8

}

註釋:
1. 分配一個日期組件對象
2. 將日期組件的年份設置爲汽車的年份
3. 使用當前日曆從日期組件對象創建日期
4. 創建本地化的格式以顯示年份
5. 創建日期格式轉換器,並按照步驟4中創建的格式進行設置
6. 從日期格式轉換器生成本地化字符串
7. 將格式字符串參數從數字修改爲字符串
8. 更換新的本地化日期字符串的year屬性

  這裏修改的是我們在信息頁面的展示,但是發現進入到編輯頁面,依然顯示的1900,將其刪了,輸入阿拉伯語的1900,重新進入後還是顯示爲1900,沒有變成阿拉伯語,我們還要繼續修改。

  修改方法與之前的fuel修改類似,我就直接貼代碼了

  1. 通過將viewDidLoad中設置self.fuelField.text的代碼替換爲如下代,顯示本地化的燃油量:
    self.yearField.text = [NSString localizedStringWithFormat:@"%d",self.currentCar.year];

  2. 在prepareForSegue:sender:中,用以下代碼替換更新self.currentCar.fuelAmount的代碼行:NSNumberFormatter *readYear = [NSNumberFormatter new];
    readYear.locale = [NSLocale currentLocale];
    [readYear setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber *YearNum = [readYear numberFromString:self.yearField.text];
    self.currentCar.year = [YearNum integerValue];

  3. 在viewWillDisapper中執行與步驟2相同的代碼修改任務。

      現在就剩下車號了,這個就留給你們自己弄咯

這裏寫圖片描述

  (PS可能會發現阿拉伯語的文字反了,那是因爲我們輸進去的就是反的。。。我也是後來才發現的,,畢竟不認識字)

  本地化就到此爲止咯(字體iOS已經自動幫我們設置好了自動對齊了,當然我們也能自己手動的去對其,這裏就不多說啦)
  
今天的介紹就到這裏咯

我的另一個博客站點:Arnold-你們好啊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章