Localization and CocoaPods

落花浮王杯 提交于 2019-11-30 01:52:10

NSLocalizedString just invokes NSBundle's localizedStringForKey:value:table: so I wrote a NSBundle category to enable looking into several bundles (which in iOS are just folders):

NSString * const kLocalizedStringNotFound = @"kLocalizedStringNotFound";

+ (NSString *)localizedStringForKey:(NSString *)key
                              value:(NSString *)value
                              table:(NSString *)tableName
                       backupBundle:(NSBundle *)bundle
{
    // First try main bundle
    NSString * string = [[NSBundle mainBundle] localizedStringForKey:key
                                                               value:kLocalizedStringNotFound
                                                               table:tableName];

    // Then try the backup bundle
    if ([string isEqualToString:kLocalizedStringNotFound])
    {
        string = [bundle localizedStringForKey:key
                                         value:kLocalizedStringNotFound
                                         table:tableName];
    }

    // Still not found?
    if ([string isEqualToString:kLocalizedStringNotFound])
    {
        NSLog(@"No localized string for '%@' in '%@'", key, tableName);
        string = value.length > 0 ? value : key;
    }

    return string;
}

Then redefined NSLocalizedString macro in my prefix file:

#undef  NSLocalizedString
#define NSLocalizedString(key, comment) \
[NSBundle localizedStringForKey:key value:nil table:@"MyStringsFile" backupBundle:AlternativeBundleInsideMain]

The same for other macros if needed (i.e. NSLocalizedStringWithDefaultValue)

SwiftArchitect

@Rivera Swift 2.0 version:

static let kLocalizedStringNotFound = "kLocalizedStringNotFound"

static func localizedStringForKey(key:String,
                                  value:String?,
                                  table:String?,
                                  bundle:NSBundle?) -> String {
    // First try main bundle
    var string:String = NSBundle.mainBundle().localizedStringForKey(key, value: kLocalizedStringNotFound, table: table)

    // Then try the backup bundle
    if string == kLocalizedStringNotFound {
        string = bundle!.localizedStringForKey(key, value: kLocalizedStringNotFound, table: table)
    }

    // Still not found?
    if string == kLocalizedStringNotFound {
        print("No localized string for '\(key)' in '\(table)'")
        if let value = value {
            string = value.characters.count > 0 ? value : key
        } else {
            string = key
        }
    }

    return string;
}
  1. You should not put any file in the Pods project, because the pod command will recreate the project again and again.

    So put the string files in your own project.

  2. If you want to ship localized string files in your own Pod, you should include it in a bundle and make sure, the bundle will be installed in your Podspec file.

For example:

def s.post_install(target)
    puts "\nGenerating YOURPOD resources bundle\n".yellow if config.verbose?
    Dir.chdir File.join(config.project_pods_root, 'YOURPOD') do
        command = "xcodebuild -project YOURPOD.xcodeproj -target YOURPODResources CONFIGURATION_BUILD_DIR=../Resources"
        command << " 2>&1 > /dev/null" unless config.verbose?
        unless system(command)
            raise ::Pod::Informative, "Failed to generate YOURPOD resources bundle"
        end

        File.open(File.join(config.project_pods_root, target.target_definition.copy_resources_script_name), 'a') do |file|
            file.puts "install_resource 'Resources/YOURPODResources.bundle'"
        end
    end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!