Multiple SQLite Databases for Multiple Languages?

I would like to implement multi-language support for my application. So I created the Localizing.strings file and all this and translated my interface. So far so good ...

Now I want to duplicate my database in order to have a * .db file for each individual language. So, I did, and then I clicked through Xcode on the “+” on the “Localization” tab. I now have a * .db file in the en.lproj and de.lproj .

My problem: if I want to copy db files to the application document directory, the * .db file is not available because it is in the * .lproj folder. Is there any command to get the correct lproj folder?

To clarify my needs: This does not work.

 [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mydatabase.db"] 

... it does:

 [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"de.lproj/mydatabase.db"] 

... but I do not want to add "de.lproj" and "en.lproj", etc. manually. Is there a way to fix it dynamically?

+6
source share
3 answers

What you want is the current language, the following code should return the code:

 NSArray *languagesArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]; NSString *currentLanguage = [languagesArray objectAtIndex:0]; 

Then you can do the following

 [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.lproj/mydatabase.db", currentLanguage]]; 

You might want to check if a path exists and is it a valid file if you don’t use any default path, for example for English (en.lproj)

Edit: There is another way you can do this using your preferred NSLocale languages, because then you will get a list of preferred languages, so some updated code for the first bit would be:

 NSArray *languagesArray = [NSLocale preferredLanguages]; NSString *currentLanguage = [languagesArray objectAtIndex:0]; 

You end up with something like this:

 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 } 

Please note that the above code has not been verified, but should be sufficient. You may need to change it a bit ...

+2
source

just follow these steps:

 NSString *dbpathResource = [[NSBundle mainBundle] pathForResource:@"databaseName" ofType:@"db"]; 

and if you have a localized .db file in xx.lproj, then the correct database will be made.

+3
source

Something like that:

 NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0]; NSString * rootPath = [[NSBundle mainBundle] resourcePath]; NSString * resourcePath = [[NSBundle mainBundle] pathForResource: @"mydatabase" ofType: @"db" inDirectory: rootPath forLocalization: language]; 
+1
source

Source: https://habr.com/ru/post/899067/


All Articles