何がしたいことは、現在の言語ロケールで、次のコードは、コードを返す必要があります:
NSArray *languagesArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languagesArray objectAtIndex:0];
あなたは、パスが存在するかどうかをチェックすることをお勧めします次
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.lproj/mydatabase.db", currentLanguage]];
を行うことができます多分英語のための1(en.lproj)
編集のようないくつかのデフォルトのパスを使用しない場合は、有効なファイルです:あなたはこの使用NSLocaleの推奨を行うことができます別の方法がありますそして、あなたが優先言語のリストを取得し、その最初のビットのためのいくつかの更新されたコードは次のようになりますので、言語:
NSArray *languagesArray = [NSLocale preferredLanguages];
NSString *currentLanguage = [languagesArray objectAtIndex:0];
最後に、あなたはそうのようなもので終わるだろう:
NSString *pathComponent = [NSString stringWithFormat:@"%@.lproj/mydatabase.db", currentLanguage];
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathComponent];
NSString *activePath = nil; // This will store the active language file
// Check if the file exists...
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
activePath = path;
} else {
activePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"en.lproj/mydatabase.db"]; // Fallback
}
上記のコードはテストされていませんが、十分なはずです。
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString * rootPath = [[NSBundle mainBundle] resourcePath];
NSString * resourcePath = [[NSBundle mainBundle] pathForResource: @"mydatabase" ofType: @"db" inDirectory: rootPath forLocalization: language];
素晴らしい答え!どうもありがとうございました! – Dude
fileExistsAtPath:pathComponentはfileExistsAtPathでなければなりません:パスが - それは完璧に動作します – Dude
いやはや!私は答えについている誰かのための訂正を反映する答えを編集しました。 –