iOS 应用程序中的手动语言选择(iPhone 和 iPad) [英] manual language selection in an iOS-App (iPhone and iPad)

查看:17
本文介绍了iOS 应用程序中的手动语言选择(iPhone 和 iPad)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:

我的 iPhone 应用程序如何告诉 iOS 用户确实在应用程序偏好设置中选择了一种与常规设置中设置的语言不同的语言?

How can my iPhone-app tell the iOS, that the user did select a language in the apps preferences, that is different from the language set in the general settings?

同一问题的其他表述:

我如何告诉系统,NSLocalizedString (@"text", @"comment"); 不应该访问系统范围内选择的语言,而是应用内选择的语言?

How can i tell the system, that NSLocalizedString (@"text", @"comment"); should not access the systemwide selected language, but the in-app-selected language?

背景,示例:

请以这种情况为例:一个德国移民的儿子住在法国东北部,毗邻卢森堡和德国.他的母语是法语,所以他确实将 iPhone 的用户界面语言设置为法语(设置 → 通用 → 国际 → 语言 → 法语).但由于他的文化背景和他居住的地区是双语的,他的德语也说得很好.但是他不会说十个单词的英语.在 iPhone(和 iPad)上,他没有机会选择第二语言,所以手机只知道他说法语.它不了解其他语言的用户技能.

Please take this situation as an example: A son of german immigrants is living in the north-east of France next to Luxemburg and Germany. His native language is French, so he did set the user-interfaces language of his iPhone to French (Settings → General → International → Language → Français). But due to his cultural background and because the region where he is living is bilingual, he also speaks German very well. But he doesn't speak ten words of English. On an iPhone (and iPad as well) he has no chance to select a second language, so the phone only knows that he spreaks french. It has no knowledge of the users skills in other languages.

现在是我的应用程序:我确实用英语和德语开发了它(德语是我的母语,英语是 IT 的标准语言).我确实根据多语言 iOS 应用程序的所有规则和最佳实践来开发它.第一"我的应用程序的语言(默认语言)是英语.

Now comes my app: I did develop it in English and German (German is my native language and English is standard-language in IT). I did develop it according to all rules and best practices for mulilingual iOS-Apps. "First" Language (default language) of my app is English.

这意味着:

如果有人在其设置中选择了英语或德语,则应用程序用户界面将自动使用所选语言.用户甚至不会注意到还有其他语言可用.

If somebody has chosen English or German in his Settings, the apps user-interface automatically will use the selected language. The user will not even notice that there are other languages available.

但如果他确实在常规设置中选择了任何其他语言(如中文、波兰语或法语),他将获得应用程序的默认语言,在我的例子中是英语.但对于我的法德朋友来说,这不是最好的选择.他想使用现有的德语版本,但似乎没有办法让用户选择这个版本.

But if he did select any other language (like Chinese, Polish or French) in the general settings, he will get the apps default-language, which, in my case, is English. But for my french-german friend this is not the best choice. He would like to use the existing german version, but there seems to be no way to let the user select this version.

添加法语翻译可以解决我们的法德朋友的问题,但不能解决说其他两种语言(例如意大利语和德语)的人的问题,而且我无法支持我的应用程序支持这个星球上使用的所有语言.将默认语言设置为德语也不是最佳选择,因为这对于说法语(作为母语)和英语(作为第二语言)的人来说会产生同样的问题.

Adding a french translation would solve the problem for our french-german friend, but not for people speaking two other languages (such as italian and german), and I can not support my app with all languages spoken on this planet. Setting the default-language to German is also not optimal, because this would rise the same problem for people speaking french (as native language) and English (as second language).

所以我认为我的应用必须能够手动选择与预选语言不同的语言.向应用程序设置面板添加语言选择不是问题.但是我如何告诉系统, NSLocalizedString (@"text", @"comment"); 不应该访问系统范围内选择的语言,而是应用内选择的语言?p>

So I think my app must have the possibility to manually select a language that is different from the pre-selected language. Adding a language-selection to the apps settings-panel ist not the problem. But how can i tell the system, that NSLocalizedString (@"text", @"comment"); should not access the systemwide selected language, but the in-app-selected language?

推荐答案

与此同时,我确实为自己的问题找到了解决方案:

In the meantime I did find a solution for my problem on myself:

我创建了一个新类LocalizeHelper":

I created a new class "LocalizeHelper":

标头 LocalizeHelper.h

//LocalizeHelper.h

#import <Foundation/Foundation.h>

// some macros (optional, but makes life easy)

// Use "LocalizedString(key)" the same way you would use "NSLocalizedString(key,comment)"
#define LocalizedString(key) [[LocalizeHelper sharedLocalSystem] localizedStringForKey:(key)]

// "language" can be (for american english): "en", "en-US", "english". Analogous for other languages.
#define LocalizationSetLanguage(language) [[LocalizeHelper sharedLocalSystem] setLanguage:(language)]

@interface LocalizeHelper : NSObject

// a singleton:
+ (LocalizeHelper*) sharedLocalSystem;

// this gets the string localized:
- (NSString*) localizedStringForKey:(NSString*) key;

//set a new language:
- (void) setLanguage:(NSString*) lang;              

@end

<小时>

实施 LocalizeHelper.m

// LocalizeHelper.m
#import "LocalizeHelper.h"

// Singleton
static LocalizeHelper* SingleLocalSystem = nil;

// my Bundle (not the main bundle!)
static NSBundle* myBundle = nil;


@implementation LocalizeHelper


//-------------------------------------------------------------
// allways return the same singleton
//-------------------------------------------------------------
+ (LocalizeHelper*) sharedLocalSystem {
    // lazy instantiation
    if (SingleLocalSystem == nil) {
        SingleLocalSystem = [[LocalizeHelper alloc] init];
    }
    return SingleLocalSystem;
}


//-------------------------------------------------------------
// initiating
//-------------------------------------------------------------
- (id) init {
    self = [super init];
    if (self) {
        // use systems main bundle as default bundle
        myBundle = [NSBundle mainBundle];
    }
    return self;
}


//-------------------------------------------------------------
// translate a string
//-------------------------------------------------------------
// you can use this macro:
// LocalizedString(@"Text");
- (NSString*) localizedStringForKey:(NSString*) key {
    // this is almost exactly what is done when calling the macro NSLocalizedString(@"Text",@"comment")
    // the difference is: here we do not use the systems main bundle, but a bundle
    // we selected manually before (see "setLanguage")
    return [myBundle localizedStringForKey:key value:@"" table:nil];
}


//-------------------------------------------------------------
// set a new language
//-------------------------------------------------------------
// you can use this macro:
// LocalizationSetLanguage(@"German") or LocalizationSetLanguage(@"de");
- (void) setLanguage:(NSString*) lang {

    // path to this languages bundle
    NSString *path = [[NSBundle mainBundle] pathForResource:lang ofType:@"lproj" ];
    if (path == nil) {
        // there is no bundle for that language
        // use main bundle instead
        myBundle = [NSBundle mainBundle];
    } else {

        // use this bundle as my bundle from now on:
        myBundle = [NSBundle bundleWithPath:path];

        // to be absolutely shure (this is probably unnecessary):
        if (myBundle == nil) {
            myBundle = [NSBundle mainBundle];
        }
    }
}


@end

<小时>

对于您想要支持的每种语言,您都需要一个名为 Localizable.strings 的文件.这与 Apples 本地化文档中的描述完全相同.唯一的区别:现在您甚至可以使用 Apple 不支持的语言,例如印地语或世界语.


For each language you want to support you need a file named Localizable.strings. This works exactly as described in Apples documentation for localization. The only difference: Now you even can use languages like hindi or esperanto, that are not supported by Apple.

举个例子,这里是我的英文版和德文版 Localizable.strings 的第一行:

To give you an example, here are the first lines of my english and german versions of Localizable.strings:

英语

/* English - English */

/* for debugging */
"languageOfBundle" = "English - English";

/* Header-Title of the Table displaying all lists and projects */
"summary" = "Summary";

/* Section-Titles in table "summary" */
"help" = "Help";
"lists" = "Lists";
"projects" = "Projects";
"listTemplates" = "List Templates";
"projectTemplates" = "Project Templates";

德语

/* German - Deutsch */

/* for debugging */
"languageOfBundle" = "German - Deutsch";

/* Header-Title of the Table displaying all lists and projects */
"summary" = "Überblick";

/* Section-Titles in table "summary" */
"help" = "Hilfe";
"lists" = "Listen";
"projects" = "Projekte";
"listTemplates" = "Vorlagen für Listen";
"projectTemplates" = "Vorlagen für Projekte";

<小时>

要使用本地化,您的应用中必须有一些设置例程,并且在您调用宏的语言选择中:


To use localizing, you must have some settings-routines in your app, and in the language-selection you call the macro:

LocalizationSetLanguage(selectedLanguage);

在此之后,您必须确保以旧语言显示的所有内容现在都以新语言重绘(隐藏文本一旦再次可见就必须重绘).

After that you must enshure, that everything that was displayed in the old language, gets redrawn in the new language right now (hidden texts must be redrawn as soon as they get visible again).

要为每种情况提供本地化文本,您永远不必将修复文本写入对象标题.始终使用宏 LocalizedString(keyword).

To have localized texts available for every situation, you NEVER must write fix texts to the objects titles. ALWAYS use the macro LocalizedString(keyword).

不要:

cell.textLabel.text = @"nice title";

做:

cell.textLabel.text = LocalizedString(@"nice title");

并且在每个版本的 Localizable.strings 中都有一个不错的标题"条目!

and have a "nice title" entry in every version of Localizable.strings!

这篇关于iOS 应用程序中的手动语言选择(iPhone 和 iPad)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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