繁体   English   中英

如何以这样一种方式提供国际化/本地化,以便将来可以随时更新属性列表?

[英]How to provide internationalization / localization in such a way, that a Property List can be updated any time in the future?

我正在寻找一种方法,在发布应用程序后为我的应用程序提供更多的本地化,而无需Apple批准我的本地化文件中的所有微小更改。 也就是说,我的应用程序可能首先只能以英语发布,但是一周后,我可能还会添加捷克语,法语和西班牙语。

因此,一般而言,您会提出什么建议? 据我所知,对于每种语言,Xcode中都有一个子目录,其中包含一个特殊的字符串plist。 但是该plist不会存储在文档目录中,并且标准实现将始终仅查看无法更改的私有目录上的这些文件,对吗? 是否有任何实用/简单的方法来实现这种灵活性,还是我应该忘了它,并对整个应用程序进行硬编码?

我有一个从服务器下载本地化字符串的应用程序。 它在应用程序的缓存目录中创建一个DownloadedBundle / [lang] .lproj目录,并将下载的Localizable.strings文件保存在该目录中。 然后,它使用DownloadedBundle目录作为路径创建一个NSBundle,并从该捆绑软件中加载资源。

一些代码片段,省略了实际的下载部分:

// Returns the path of the downloaded bundle:
- (NSString*) downloadedBundlePath {
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString* path = [paths objectAtIndex:0];
    return [path stringByAppendingPathComponent:@"DownloadedBundle"];
}

// Returns the downloaded bundle:
- (NSBundle*) downloadedBundle {
    if (downloadedBundle == nil) {
        downloadedBundle = [[NSBundle bundleWithPath:self.downloadedBundlePath] retain];
    }
    return downloadedBundle;
}

// Returns a string from the downloaded bundle:
- (NSString*) localizedStringForKey:(NSString*)key {
    return [self.downloadedBundle localizedStringForKey:key value:nil table:nil];
}

// Saves the downloaded strings file in the bundle:
- (void) saveDownloadedFile:(NSString*)downloadedFilePath inLanguage:(NSString*)language {
    NSFileManager* fileManager = [[NSFileManager alloc] init];
    NSError* error = nil;
    NSString* lprojPath = [[self downloadedBundlePath] 
                           stringByAppendingPathComponent:[language stringByAppendingString:@".lproj"]];
    if (![fileManager createDirectoryAtPath:lprojPath withIntermediateDirectories:YES attributes:nil error:&error]) {
        NSLog(@"Failed to create a directory at %@: %@", lprojPath, error);
        return;
    }

    // Move the downloaded file to be the new strings file:
    NSString* stringsFilePath = [lprojPath stringByAppendingPathComponent:@"Localizable.strings"];
    if ([fileManager fileExistsAtPath:stringsFilePath]) {
        if (![fileManager removeItemAtPath:stringsFilePath error:&error]) {
            NSLog(@"Failed to remove the old strings file at %@: %@", stringsFilePath, error);
            return;
        }
    }
    if (![fileManager moveItemAtPath:downloadedFilePath toPath:stringsFilePath error:&error]) {
        NSLog(@"Failed to move the new strings file from %@ to %@: %@", moveItemAtPath:downloadedFilePath, stringsFilePath, error);
        return;
    }
}

免责声明:此代码是我实际拥有的简化版本,并且未经此形式的测试。 但希望它能提供一些想法。

为什么要动态地更新本地化字符串? 如果唯一需要考虑的是添加更多的本地化,则可以更新应用程序并将其重新提交给appstore。 由于此过程需要时间,因此您不会每隔3-5天这样做一次,便会完成新的本地化,而是将它们全部打包在一个更新中。

副作用是,您的应用程序将进入更新列表,并在顶部浮动一会儿。

没错,您不能更改存储在“资源”文件夹(捆绑包)中的项目。 您可以将本地化存储在documents文件夹中,但随后必须更改访问该文件夹的方式,而不是捆绑软件中的一个文件夹。 对我来说,这还不是很简单。

您还可以远程存储数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM