0
A
答えて
0
ことは、これを試してみてください:
// reading the language from the preferredLanguages in the bundle project architecture.
#define currentLanguageBundle [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"]]
#define CurrentNSLocalizedString(key) NSLocalizedStringFromTableInBundle(key, nil, currentLanguageBundle, @"")
// you can set language in the same location preferred settings like this :
[[NSUserDefaults standardUserDefaults]setObject:lang forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
// lang can be :
@"fr"
@"en"
@"de"
@"es"
@"it"
// etc ...
、あなたはこのようなのLocalizable.stringsに設定された文字列のtraductionを呼び出すことができます。CurrentNSLocalizedString(@"my_text")
そしてここでは、あなたの文字列の翻訳を使用するためにいくつかのリンクです: https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/LocalizingYourApp/LocalizingYourApp.html
https://www.raywenderlich.com/64401/internationalization-tutorial-for-ios-2014
0
言語を変更する場合アプリケーションで設定します。考慮すべき2つのソリューションがあります。
最初のものは
.H
@interface NSBundle (HLanguage)
+(void)setLanguage:(NSString*)language;
@end
.M
#import "NSBundle+HLanguage.h"
#import <objc/runtime.h>
static const char _bundle=0;
@interface HBundle : NSBundle
@end
@implementation HBundle
-(NSString*)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
NSBundle* bundle=objc_getAssociatedObject(self, &_bundle);
return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [super localizedStringForKey:key value:value table:tableName];
}
@end
@implementation NSBundle (HLanguage)
+(void)setLanguage:(NSString*)language
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^
{
object_setClass([NSBundle mainBundle],[HBBundle class]);
});
objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
このアプローチのように、良好かつ簡単な方法で、特定のロケール・ソースをロードするメインバンドルを強制されますサポートされているローカリゼーションを追加するだけです。
2番目の言語は柔軟性が高く、同じEngロケールなどの言語設定やコンテンツのいずれかをサポートできますが、ドイツの英語ではシンガポールで英語と異なるコンテンツが表示されます。簡単に言えば、独自のバンドルリソースを作成し、サポートされているすべての言語を入力してから、NSLocalizedStringWithDefaultValue
またはNSLocalizedStringFromTableInBundle
を使用して、選択した言語の「バンドル」と「テーブル」を指定します。
これは役に立ちます。
あなたはこのようなリンクを見つけようとしましたか? – dorukayhan
通常、ローカライズを実装すると、アプリは最初に端末の言語に準拠しています。明示的に言語を指定する場合は、アプリケーションのバンドルを指定する必要があります(例:let path = NSBundle.mainBundle()、pathForResource(language、ofType: "lproj"、inDirectory:nil、forLocalization:language){ LocalizationHelper.bundle = NSBundle(パス:パス) } – Happiehappie
言語はXcodeで指定された言語コードで、ファイル名は "en.lprog"のようになります。 – Happiehappie