用于多种语言的多个SQLite数据库? [英] Multiple SQLite Databases for Multiple Languages?

查看:211
本文介绍了用于多种语言的多个SQLite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的应用程序实现多语言支持.因此,我创建了Localizing.strings文件以及所有这些内容并转换了我的界面.到目前为止一切顺利……

I would like to implement a multi language support for my app. So I created the Localizing.strings file and all that stuff and translated my interface. So far so good …

现在,我想复制我的数据库,以便为每种单一语言提供一个* .db文件.因此,我这样做了,然后我通过XCode在本地化"选项卡下的"+"上单击了.现在,我的en.lprojde.lproj文件夹中有一个* .db文件.

Now I want to duplicate my database in order to have a *.db-file for every single language. So I did and then I clicked via XCode on the "+" under the Localization tab. I now have a *.db-file in my en.lproj and de.lproj folder.

我的问题:如果我要将db文件复制到应用程序的文档目录中,则* .db文件当然不可用,因为它位于* .lproj文件夹中.是否有任何命令获取正确的lproj文件夹?

My problem: If I want to copy the db-files to the app's documents directory the *.db file is not available of course because it is in the *.lproj-folder. Is there any command to get the right lproj-folder?

要阐明我的需求: 这不起作用

To clarify my needs: This doesn't work

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

…这样做:

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

…但是我不想手动添加"de.lproj"和"en.lproj"等.有什么办法可以动态修复它?

… but I don't want to add the "de.lproj" and "en.lproj" etc. manually. Is there any way to fix it dynamically?

推荐答案

您想要的是当前的语言环境,以下代码应返回该代码:

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

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

然后您可以执行以下操作

You can then do the following

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

您可能要检查该路径是否存在并且是有效文件,否则请使用一些默认路径,例如英语(en.lproj)

You may want to check if the path exists and is a valid file, if not maybe use some default path like the one for English (en.lproj)

还有另一种方法可以使用NSLocale的首选语言来完成此操作,因为这样您将获得首选语言的列表,因此第一位的一些更新代码将是:

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

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

最后,您将得到如下结果:

In the end, you'd end up with something like so:

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, the above code is untested but should suffice. You may need to modify it a little...

这篇关于用于多种语言的多个SQLite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆