Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在静态库中从我的应用加载本地化字符串?_Ios_Objective C_Xcode_Localization_Translation - Fatal编程技术网

Ios 在静态库中从我的应用加载本地化字符串?

Ios 在静态库中从我的应用加载本地化字符串?,ios,objective-c,xcode,localization,translation,Ios,Objective C,Xcode,Localization,Translation,我有一个分层的项目,底部的项目作为静态库包含在顶部的项目中 问题是,我需要在静态库中本地化字符串,使用我的应用程序上层中的翻译 这有可能吗?我设法做到这一点的方法是从bundle加载字符串,而不是使用NSLocalizedString: 您可以在静态库中创建LanguageAgent,并在该库中添加捆绑资源。然后使用这样的函数来获取localizedString。在我的应用程序中,我通过不同的表来分隔语言,请参见下图中名为“Dictionaire”的表。语言包中可以有多个表 -(NSString

我有一个分层的项目,底部的项目作为静态库包含在顶部的项目中

问题是,我需要在静态库中本地化字符串,使用我的应用程序上层中的翻译


这有可能吗?

我设法做到这一点的方法是从bundle加载字符串,而不是使用NSLocalizedString:


您可以在静态库中创建LanguageAgent,并在该库中添加捆绑资源。然后使用这样的函数来获取localizedString。在我的应用程序中,我通过不同的表来分隔语言,请参见下图中名为“Dictionaire”的表。语言包中可以有多个表

-(NSString*) myLocalizedStringForKey:(NSString*) key ofTable:(NSString*)tableName {
    //I save selected language in my NSUserDefaults.
    NSString *selectedLanguage = [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultLanguage"];

    if (selectedLanguage == nil) {
        [[NSUserDefaults standardUserDefaults] setValue:@"en" forKey:@"DefaultLanguage"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        selectedLanguage = [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultLanguage"];
     }

    NSString *langBundleNew = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/langs/Languages.bundle/%@.lproj/",selectedLanguage]; //use your path to the Languages.bundle here. 

    if ([[NSFileManager defaultManager] fileExistsAtPath:langBundleNew]) {
        NSBundle *aBundle = (NSBundle*)[self.dictLangueBundle objectForKey:selectedLanguage];
         NSString* str=[aBundle localizedStringForKey:key value:@"[string not defined]" table:tableName];
         return str;
    } else {
        return @"[]";
    }
}
我的语言包类似于这样:“Dictionaire”=表的名称

以下是my Dictionnaire.strings中“en.lproj”的内容示例:


这里有一件事我不明白,那就是languages.bundle存在于哪里?它是在静态库的范围内,还是在实际使用库的应用程序内?谢谢您可以将languages.bundle集成到静态库中,或将此文件添加到主项目中。在我的代码中,我将其添加到我的主项目中,然后当应用程序启动时,我将其移动到库中的一个文件夹中。因为我希望这个文件可以修改。看看这个:是的,我明白。但是你必须从主应用程序复制到一个文件夹,并且事先知道这个应用程序在你的库中是什么。对于我发布的代码片段,您不在乎每个文件在哪里,您只从捆绑包中获得它。无论如何,谢谢你的主意!
-(NSString*) myLocalizedStringForKey:(NSString*) key ofTable:(NSString*)tableName {
    //I save selected language in my NSUserDefaults.
    NSString *selectedLanguage = [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultLanguage"];

    if (selectedLanguage == nil) {
        [[NSUserDefaults standardUserDefaults] setValue:@"en" forKey:@"DefaultLanguage"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        selectedLanguage = [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultLanguage"];
     }

    NSString *langBundleNew = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/langs/Languages.bundle/%@.lproj/",selectedLanguage]; //use your path to the Languages.bundle here. 

    if ([[NSFileManager defaultManager] fileExistsAtPath:langBundleNew]) {
        NSBundle *aBundle = (NSBundle*)[self.dictLangueBundle objectForKey:selectedLanguage];
         NSString* str=[aBundle localizedStringForKey:key value:@"[string not defined]" table:tableName];
         return str;
    } else {
        return @"[]";
    }
}