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-你们好啊

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