根据所需语言检索文件

时间:2014-05-02 18:28:55

标签: ios localization

我想根据用户当前使用的语言获取法律文档。

我的代码就是这个:

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
if (sender == privacyPolicyButton) {
        ppPath = [libraryDirectory stringByAppendingPathComponent:@"privacy_policy.html"];

    } else if (sender == termsOfServiceButton) {
        ppPath = [libraryDirectory stringByAppendingPathComponent:@"terms.html"];

    } else if (sender == endUserButton) {
        ppPath = [libraryDirectory stringByAppendingPathComponent:@"eula.html"];

    }

    NSData *ppData = nil;
    policyView.scrollView.scrollEnabled = NO;

    [loadingIndicator startAnimating];
    policyView.hidden = closePolicyButton.hidden = NO;
    privacyPolicyButton.hidden = termsOfServiceButton.hidden = endUserButton.hidden = YES;

    ppData = [NSData dataWithContentsOfFile:ppPath];
    NSLog(@"%@", ppPath);

    [policyView loadData:ppData MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:nil];

我有西班牙语和英语的每个文档的版本,我如何告诉使用西班牙语版本的路径?

This is how the files look

1 个答案:

答案 0 :(得分:1)

NSBundle课程为您完成此任务。假设您的HTML文件是应用程序资源包的一部分,您可以执行以下操作:

NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"terms" ofType:@"html"];

这将根据用户的区域设置/语言设置为您提供正确的terms.html文件的路径。