iOS PHAssetCollection localizedTitle 总是返回英文名称

iOS PHAssetCollection localizedTitle always returning english name

我正在使用 Apples Photos Framework 将 PHAssetCollections 加载到我的应用程序中。 我总是得到聪明的英文名字collections。例如,我得到 'Favorites' 而不是 'Favoriter' (瑞典语)。我认为 localizedTitle 属性 会 return 模拟器或 iPhone 运行 的语言。 (运行 在 iPhone 上使用瑞典语和地区)。

有没有人遇到过这个? 示例代码:

NSArray *collectionsFetchResults;
NSMutableArray *localizedTitles = [[NSMutableArray alloc] init];

PHFetchResult *smartAlbums = [PHAssetCollection       fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
                                                                            subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
PHFetchResult *syncedAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                                       subtype:PHAssetCollectionSubtypeAlbumSyncedAlbum options:nil];
PHFetchResult *userCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];

// Add each PHFetchResult to the array
collectionsFetchResults = @[smartAlbums, userCollections, syncedAlbums];

for (int i = 0; i < collectionsFetchResults.count; i ++) {

    PHFetchResult *fetchResult = collectionsFetchResults[i];

    for (int x = 0; x < fetchResult.count; x ++) {

        PHCollection *collection = fetchResult[x];
        localizedTitles[x] = collection.localizedTitle;
        NSLog(@"%@", collection.localizedTitle); //<= Always prints english names
    }
}

您的问题可能是 none 显式或隐式 return built-in 收藏夹智能相册。 (此外,将您的代码简化为仅检查一次提取的结果有助于诊断,因此您可以找出哪一个包含 non-localizing "Favorites"。)

如果您明确想要收藏夹 collection,请使用 PHAssetCollectionSubtypeSmartAlbumFavorites collection 子类型搜索它。 (它不应该是搜索常规相册或同步相册的结果,如果在顶级用户 collection 中看到它,我会感到惊讶,因为只有 return user-created collections.)

如果明确请求收藏夹智能相册 return 正在 non-localizing 版本,或者您的其他抓取之一正在 return 收藏夹智能相册但它不是本地化,这可能是 Apple 的一个错误 — 我建议 telling them about it

之所以 collection.localizedTitle 没有 return 任何其他语言,是因为您的应用只有一种本地化版本,即 English。这绝对是一个 Apple Bug,但这里有一个 hack,请按照以下步骤操作。

  1. 除了您已有的 en.lproj 文件夹之外,创建相关文件夹 sv.lproj
  2. 在文件夹中添加一个空白文本文件,然后将文件添加到您的项目中。
  3. 执行第 2 步。您正在为您的项目添加瑞典语支持。
  4. 运行 您的代码,它应该 return 瑞典语标题。

更新

如果您想在添加空白文本后轻松添加更多语言,您可以像其他答案中显示的那样通过选择项目添加,然后在本地化部分添加语言并再次选择空白文件,因为我们不想本地化其他文件。

希望对你有所帮助,如有疑问请在此提问。

干杯。

我认为您应该确保您的应用程序支持瑞典语。 例如添加你想要支持瑞典语的语言

项目 -> 本地化 -> "+ 中文(简体)"enter image description here