Tml 0.2.2

Tml 0.2.2

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布上次发布2016年2月

Michael BerkovichPasha维护。



 
依赖项
MPColorTools~> 1.6
MBProgressHUD~> 0.9
 

Tml 0.2.2

Tml for Objective C

Tml SDK for Objective C 是一个用于 iOS 应用程序的集成云端翻译解决方案。

它减少了进行移动应用程序国际化本地化的步骤数量。

Tml SDK 与 TranslationExchange.com 服务集成,您可以在其中管理工作翻译的整个流程 - 启用语言,邀请翻译者,管理翻译键等。

您永远不会触及资源包文件 - 翻译键会根据 SDK 在应用源代码中即时提取并保持更新。

一旦您的应用被翻译,翻译就会由 SDK 自动下载并安装。您不需要将新应用程序提交到 App Store。您只需在 TranslationExchange.com 上启用新语言,它就会立即在您的应用程序中可用。

示例应用程序

要运行示例项目,请按照以下步骤操作

$ git clone https://github.com/translationexchange/tml-objc.git
$ cd tml-objc/Project
$ pod install
$ open Demo.xcworkspace

运行应用。打开语言选择器并更改语言。

另一个示例应用程序位于这里

https://github.com/translationexchange/tml-objc-samples-wammer

要求

iOS 7
CocoaPods

安装

集成

在您继续集成之前,请访问 https://TranslationExchange.com - 打开账户并注册您的应用程序。一旦注册了您的应用程序,它将分配一个唯一的密钥和秘密。

您只需要在测试或翻译应用程序时提供秘密。拥有秘密的应用程序可以在服务器上注册新的翻译键。当您发布应用程序时,您应该移除秘密并只提供密钥。

Tml SDK 默认带有英文语言配置。因此,如果您的应用的基本语言是英文,您可以在不初始化 SDK 的情况下测试 SDK。您可以得到 TML 的全部力量和国际化宏,这将提高您的代码并提供用于处理英语语言的功能强大的实用程序。Tml 提供默认的屈折变化器、复数化器、上下文化器和语言情况。

另一方面,如果您想利用与翻译平台、翻译记忆和数据分析的持续集成,则应初始化Tml SDK。

#import "Tml.h"

....


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    ... 

[Tml sharedInstanceWithToken: YOUR_TOKEN];

....

return YES;
}

它是如何工作的?

Tml SDK为您完成所有艰难的工作。当您使用Tml的宏时,库会自动将翻译密钥注册到翻译交流服务中,并在运行时为您生成应用的资源块。Tml会通过从服务中下载数据保持最新版本的翻译来确保您的本地缓存是最新的。在运行时,您的应用将使用本地应用的翻译缓存,这样您的用户将不会体验到任何延迟。

您有选项在发布应用之前预先缓存所有翻译。在这种情况下,SDK将以离线模式运行。但保持SDK连接状态允许您发布新语言而不需要重新构建您的应用。您还将能利用分析功能来了解您的应用使用了哪些语言,用户默认语言是什么,用户来自哪里等信息。

国际化与TML

如果您的应用已经使用标准 NSLocalizedString 方法国际化,您只需在您的 .m 文件中导入 Tml.h 头文件,Tml 就会接管国际化宏。

#import "Tml.h"

Tml 还提供了自己的国际化宏,它增强了标准 iOS 宏。

TmlLocalizedString(label)

示例

TmlLocalizedString(@"Hello World")

此宏类似于 NSLocalizedString,但它不需要将可选注释作为 nil 传递。

TmlLocalizedStringWithDescription(label, description)

示例

TmlLocalizedStringWithDescription(@"Invite", @"Invite someone to join the group")

TmlLocalizedStringWithDescription(@"Invite", @"An invite received from a friend")

与对方不同,其中第二个参数是对翻译者的注释,Tml 使用描述来情景化密钥。所以上面的示例实际上会注册两个不同的密钥,每个密钥都有自己的翻译。

TmlLocalizedStringWithTokens(label, tokens)

示例

TmlLocalizedStringWithTokens(@"You have {count || message}.", @{@"count": @4})

TmlLocalizedStringWithTokens(@"Hello {user}.", @{@"user": @"Michael"})

User *user  = [[User alloc] initWithName: @"Michael" gender: @"male"];

// will use [user description] for substitution value
TmlLocalizedStringWithTokens(@"Hello {user}.", @{@"user": user})

// second parameter is used for substitution value
TmlLocalizedStringWithTokens(@"Hello {user}.", @{@"user": @[user, user.name]})
TmlLocalizedStringWithTokens(@"Hello {user}.", @{@"user": @[user, @"Mike"]})

NSDictionary *user = @{@"name": @"Michael", @"gender": @"male"};

// can be used for context rules, but not substitution value
TmlLocalizedStringWithTokens(@"{user | Born On}.", @{@"user": user})

TmlLocalizedStringWithTokens(@"Hello {user}.", @{
            @"user": @{@"object": user, @"property": @"name"}
})

TmlLocalizedStringWithTokens(@"Hello {user}.", @{
            @"user": @{@"object": user, @"value": @"Michael"}
})

标记大致可以通过多种方式传递。如果标记是原始类型,它将被用于情景规则和显示值。如果是类或结构,您可以分别使用对象进行情景规则和用于替换标记的值。

TmlLocalizedStringWithDescriptionAndTokens(label, description, tokens)

示例

TmlLocalizedStringWithDescriptionAndTokens(
            @"Hello {user}",
            @"A greeting message", 
            @{@"user": @"Michael"}
)

与上面两个示例相同——允许您提供描述和标记。

TmlLocalizedStringWithDescriptionAndTokensAndOptions(label, description, tokens, options)

示例

TmlLocalizedStringWithDescriptionAndTokensAndOptions(
            @"Hello {user}", 
            @"A greeting message", 
            @{@"user": @"Michael"},
            @{@"level": @5, @"max-length": @20}
)

只有具有特定级别的翻译者才能翻译具有特定级别的密钥。约束表明,此密钥的翻译可能不超过20个字符。更多详情请参阅 wiki.translationexchange.com。

TmlLocalizedStringWithTokensAndOptions(label, tokens, options)

示例

TmlLocalizedStringWithTokensAndOptions(
            @"You have {count || message}.", 
            @{@"count": @4}, 
            @{@"max-length": @20}
)

允许您跳过描述。

TmlLocalizedStringWithOptions(label, options)

示例

TmlLocalizedStringWithOptions(@"Hello World", @{@"max-length": @20})

允许您跳过描述和标记。

所有上述宏都假定您正在处理HTML,并将返回带装饰标记的HTML标记。例如

TmlLocalizedStringWithTokens(
    @"You have completed [bold: {count || mile}] on your last run.", 
    @{@"count": @4.2}
)

将结果为

"You have completed <strong>4.2 miles</strong> on your last run."

除非您在浏览器中渲染它,否则不应该使用带有上述方法的装饰标记。相反,您应使用 Attributed String 宏。

默认标记

对于您想要在整个应用中一次定义并重复使用的标记,您可以在Tml配置中预先定义它们,使用以下方法

[Tml configure:^(TmlConfiguration *config) {
    [config setDefaultTokenValue: @"<strong>{$0}</strong>"
                         forName: @"bold"
                            type: @"decoration"
                          format: @"html"];

    [config setDefaultTokenValue: @"<span style='color:green'>{$0}</span>"
                         forName: @"green"
                            type: @"decoration"
                          format: @"html"];

    [config setDefaultTokenValue: @{
                                     @"font": @{
                                        @"name": @"system",
                                        @"size": @12, 
                                        @"type": @"italic"
                                     }, 
                                     @"color": @"blue"
                                   }
                         forName: @"bold"
                            type: @"decoration"
                          format: @"attributed"];

    [config setDefaultTokenValue: @{
                                     @"shadow": @{
                                        @"offset": @1,1,
                                        @"radius": @0.5,
                                        @"color": @"grey"
                                     },
                                     @"color": @"black"
                                   }
                         forName: @"shadow"
                            type: @"decoration"
                          format: @"attributed"];

    [config setDefaultTokenValue: @"My App Name" 
                         forName: @"app_name" 
                            type: @"data"];
}];

或者,您可以直接内联提供标记值,这将覆盖默认的标记定义。

以下示例将使用前面预定义的标记。

AttributedString 宏

AttributedString 宏与上面的宏非常相似,但总是返回 NSAttributedString 而不是 plain NSString。

TmlLocalizedAttributedString(label)

示例

TmlLocalizedAttributedString(@"Hello World")
TmlLocalizedAttributedString(@"Hello [bold: World]")
TmlLocalizedAttributedString(@"This [bold: technology is [shadow: very cool]].")

请注意,“非常酷”将是加粗并带有阴影。嵌套标记会继承父标记的特性。

其他 Attributed String 宏如下

TmlLocalizedAttributedStringWithDescription(label, description)

TmlLocalizedAttributedStringWithDescriptionAndTokens(label, description, tokens)

TmlLocalizedAttributedStringWithDescriptionAndTokensAndOptions(label, description, tokens, options)

TmlLocalizedAttributedStringWithTokens(label, tokens)

TmlLocalizedAttributedStringWithTokensAndOptions(label, tokens, options)

TmlLocalizedAttributedStringWithOptions(label, options)

上述使用HTML作为装饰的示例现在可以用以下方法重写

TmlLocalizedAttributedStringWithTokens(
    @"You have completed [bold: {count || mile}] on your last run.", 
    @{@"count": @4.2}
)

这将生成一个具有以下值的 NSAttributedString

"You have completed **4.2 miles** on the last run."

NSAttributedString 与 TmlLocalizedAttributedString 的比较

使用 TML 与 NSAttributedString 的优点是标签在上下文中得到翻译。如果您在没有使用 TML 的情况下尝试上面的示例,您将得到类似以下代码的结果

NSDictionary *bold = @{[UIFont boldSystemFontOfSize:@12], NSFontAttributeName};

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] init];
[str appendString : NSLocalizedString(@"You have completed ")];

if (distance_in_miles == 1)
   [str appendAttributedString:
        [[NSAttributedString alloc] initWithString: NSLocalizedString(@"1 mile")]
                    attributes: bold];
else
   [str appendAttributedString: 
        [[NSAttributedString alloc] initWithString:
            [NSString stringWithFormat: NSLocalizedString(@"%d miles"), distance_in_miles]]
                    attributes: bold];

[str appendString: NSLocalizedString(@" on your last run.")];

上述代码有以下问题

  • 对于具有更复杂数字规则的语言,如俄语或阿拉伯语,(distance_in_miles == 1) 的检查会失败。
  • "You have completed " 和 " on your last run" 会翻译到整个句子的上下文之外。在某些语言中,一些单词必须调换顺序。因此,它在上下文化和构成都失败。
  • "1 mile" 和 "%d miles" 也翻译到了上下文之外。

上述所有代码都可以用 TML 的一行代码替换

TmlLocalizedAttributedStringWithTokens(
    @"You have completed [bold: {count || mile}] on your last run.", 
    @{@"count": @4.2}
)

这可以轻松翻译成俄语

"Вы пробежали [bold: {count || милю, мили, миль}] в вашем последнем забеге."

由于像希伯来语这样的语言依赖于查看用户的性别,所以在您提到 "You" 的时候,您应该始终传递 viewing_user 作为令牌。

TmlLocalizedAttributedStringWithTokens(@"You have completed [bold: {count || mile}] on your last run.", @{@"count": @4.2, @"viewing_user": currentUser})

或者更好的是,在配置中设置它,然后您永远不必将其作为令牌传递

[Tml configure:^(TmlConfiguration *config) {
    config.viewingUser = currentUser;
}];

这里有一个更复杂的示例

TmlLocalizedAttributedStringWithTokens(
        @"[bold: {user}] has completed [bold: {count || mile}] on {user| his, her} last run.",
        @{ 
            @"user": friend,
            @"count": @4.2,
            @"link": @{@"color": @"blue"}
            @"bold": @{@"font":@{@"name": @"system", @"size": @12, @"type": @"bold"}}
        }
)

在英语中,这将呈现为

"**Michael** has completed **4.2 miles** on his last run."

翻译成俄语后

"[link: {user}] {user | пробежал, пробежала} [bold: {count || милю, мили, миль}] в своем последнем забеге."

将呈现为

"**Michael** пробежал **4.2 мили** в своем последнем забеге."

翻译 UIView

Tml 可以通过一行代码自动为您翻译视图。考虑以下示例

#import "UIViewController+Tml.h"

...

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(localize)
                                                 name: TmlTranslationsLoadedNotification
                                               object: self.view.window];

    [self localize];
}


- (void) localize {
    TmlLocalizeView(self.view);
}

...

上述代码将在选择新的语言时每次翻译整个用户界面。

语言选择器

Tml SDK 随带一个语言选择器,您可以在应用程序中使用它。要从视图控制器打开语言选择器,请使用以下代码

#import "TmlLanguageSelectorViewController.h"

[TmlLanguageSelectorViewController changeLanguageFromController:self];

应用程序内翻译器

Tml 随带一个应用程序内翻译器,因此您的应用程序用户可以在您的应用程序内注册并翻译您的应用程序。要打开应用程序内翻译器,请使用以下代码

#import "TmlTranslatorViewController.h"

[TmlTranslatorViewController translateFromController:self];

链接

  • 在 TranslationExchange.com 注册:[http://translationexchange.com](http://translationexchange.com)

  • 阅读 TranslationExchange 的文档:[http://www.translationexchange.com/docs](http://www.translationexchange.com/docs)

  • 在 Twitter 上关注 TranslationExchange:[https://twitter.com/translationx](https://twitter.com/translationx)

  • 在 Facebook 上与 TranslationExchange 联系:[https://#/translationexchange](https://#/translationexchange)

  • 如果您有任何问题或建议,请联系我们:[人工验证邮箱](javascript:void(0);)

版权和许可

版权 (c) 2015 TranslationExchange.com

特此授予任何获得本软件及其相关文档文件("软件")副本的任何人("个人")免费使用软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、转授和/或销售软件副本的权利,以及允许向软件提供者提供软件的个人使用上述权利,但受以下条件约束

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按照“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论是在合同转让、侵权或其他情况下产生,无论是在与软件相关的使用中产生的,还是与软件的使用或处理相关的。