DPLocalization 1.13

DPLocalization 1.13

测试已测试
Lang语言 CC
许可证 MIT
释放最后发布2019年7月

Dmitriy Petrusevich维护。



  • 作者:
  • Dmitriy Petrusevich

Build Status Carthage compatible Swift Package Manager compatible Version License Platform

DPLocalization

DPLocalization提供了一种简单的方法来更改应用程序中的本地化。

用法

### 动态本地化

  1. 在代码中使用autolocalizationKey属性,或者在xib中以强user defined runtime attribute使用
	myLabel.autolocalizationKey = @"my_label_localization_key";
	myButton.autolocalizationKey = @"my_button_name";
  1. 使用-[setupAutolocalizationWithKey:keyPath:]-[setupAutolocalizationWithKey:keyPath:arguments:]方法中的NSObject
	[myLabel setupAutolocalizationWithKey:@"my_label_localization_key" keyPath:@"text"];
	[myLabel setupAutolocalizationWithKey:@"my_label_localization_template_key" keyPath:@"text" arguments:@[@"first", @"second"]];

现在在改变语言后(例如:运行dp_set_current_language(@"de"))所有设置为动态本地化的对象都会更新它们的内容。

### 静态本地化

对于静态本地化,选择语言(例如:运行dp_set_current_language(@"de"))并在NSLocalizedString()中使用DPLocalizedString()宏。

### 代理对象

使用DPAutolocalizationProxy类方法或它们的替代方法。

    NSString *str = [NSString autolocalizingStringWithLocalizationKey:@"language"];

    dp_set_current_language(@"en");
    CFShow((__bridge CFTypeRef)(str));
    dp_set_current_language(@"ru");
    CFShow((__bridge CFTypeRef)(str));

输出示例

	English
	Russian

复数规则

所有支持的语言列表。当前库仅支持基数类型。库使用的是更改后的文件(Stringsdict 文件格式)。

  • NSStringFormatValueTypeKey 被忽略
  • 格式指定符 必须是 'Objective-C 对象'

格式化的字符串

某些控件(自 iOS 6 起支持)支持属性化值。您可以使用特殊格式 (<属性>{字符串}) 指定属性,这还需要将 isAttributedKey 属性设置为 YES。属性将应用于大括号 {} 内的字符串,其他部分将使用控件指定的参数。例如

/* Localizable.strings */
"TITLE" = "English <color=12,56,189 size=12>{(en)}";
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
myLabel.autolocalizationKey = @"TITLE";
myLabel.isAttributedKey = YES;

将设置 attributedText 属性,其字符串可以描述为

English {
    NSColor = "UIDeviceWhiteColorSpace 0 1";
    NSFont = "<UICTFont: 0x79743450> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt";
}(en){
    NSColor = "UIDeviceRGBColorSpace 0.0470588 0.219608 0.741176 1";
    NSFont = "<UICTFont: 0x79644cc0> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
}

可用的属性

  • name - 设置字体名称 - <name=\"Courier-BoldOblique\">{字符串}
  • size - 设置字体大小 - <size=12.0>{字符串}<size=12>{字符串}
  • color - 以 RGB 或 RGBA 格式设置文本颜色 - <color=12,56,189>{字符串}<color=12,56,189,255>{字符串}
  • traits - 设置字体属性:b(粗体),i(斜体);以及文本属性 u(下划线),s(删除线),m(等宽);
  • kern - 设置字体间距
  • link - 设置链接属性 - <link=\"https://github.com/nullic/DPLocalizationManager\">{链接到 DPLocalizationManager}
  • spacing - 设置段落之间的空间
  • linespacing - 设置行间距
  • alignment - 设置段落的对齐方式 <alignment=center>{居中\n}<alignment=left>{左}. 有效的值:left center right justified natural. 由于 NSParagraphStyle 的行为,每个段落应以 \n 结尾,并且每个部分如果有在任何地方使用本地化字符串,应具有对齐方式。

替换

您可以使用以下格式指定替换:[<替换者>]。此功能也需要将 isAttributedKey 属性设置为 YES。示例

/* Localizable.strings */
"TITLE_WITH_IMAGE" = "Title [<img=\"image_name_here\">]";
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
myLabel.autolocalizationKey = @"TITLE_WITH_IMAGE";
myLabel.isAttributedKey = YES;

可用的替换者

  • img - 插入名为的图片 - [<img=\"image_name_here\"> size=(16,16) offset=(0,0)] - sizeoffset 都是可选参数。默认值 size 为 (image.width, image.height);offset 为 (0, font.descender)

安装

CocoaPods

DPLocalization 通过 CocoaPods 提供,要安装它,只需将以下行添加到您的 Podfile 中

pod "DPLocalization"

Carthage

要使用 Carthage 将 DPLocalization 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它

github "nullic/DPLocalizationManager"

运行 carthage update 来构建框架,并将构建的 DPLocalization.framework 拖入您的 Xcode 项目。

本地化检查

dpstrings.py 是一个 Python 脚本,用于检查库的常见用法。它扫描 {some} 文件夹中的源代码和资源,并对常见错误进行验证(缺少键,重复)。

使用示例: python dpstrings.py -p "MyProjectPath" -l "en;ru;"
更多信息: python dpstrings.py --help

许可证

DPLocalization 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。