在ios中简单的阿拉伯语单词到英语?

时间:2016-07-18 10:19:53

标签: ios objective-c

我想更改一个句子,例如:

  

مرحبا

会变成:

  

马哈巴

或者我想更改一个句子,例如:

  

kon'nichiha

会变成:

  

kon nichiha

有没有简单的方法可以在obj c中执行此操作? 谢谢你提前。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我创建了示例项目,并尝试为您的问题找到解决方案。我得到了它非常好。它运行正常。我使用 Go 英语并将其转换为阿拉伯语اذهب< /强>

第一步:在本地化中添加阿拉伯语 信息(项目 - &GT;信息 - &GT;位置)

Click Project
Then click Info
Then expand Localization.
Finally click + now choose Arabic  

enter image description here

Bellow本地化你可以看到+现在点击+。点击它会显示弹出框并从中选择阿拉伯语

enter image description here

第二步:创建本地化字符串文件

Create New File - iOS ->Resource->Strings File

enter image description here

Then Click Next now give File name.
So now it seems to be FileName.strings(Arabic)

**第三步:当本地化字符串文件选择**时单击文件检查器

Click Localization

enter image description here

There is 3 boxes are there 
Base,English,Arbic with Empty checkbox(By default the Base Checkbox is checked)
see the below checkboxes.

enter image description here

Now click Arabic and English checkboxes

enter image description here

阿拉伯语本地化文件如下所示

enter image description here

我为本地化语言创建了Header文件。

enter image description here

名称上方的头文件是 - LanguageHeader

现在我在LanguageHeader文件中设置英语和阿拉伯语

enter image description here

在我们开始编码之前,我想展示我的故事板设计

enter image description here

最后代码在

下面

首先是NSObject类的Localization类

Localization.h

#import <Foundation/Foundation.h>
#import "LanguageHeader.h"

@interface Localization : NSObject
+(Localization *)sharedInstance;
+(NSString*) strSelectLanguage:(int)curLang;
+(NSString*) languageSelectedStringForKey:(NSString*) key;

@end

Localization.m

#import“Localization.h”     int currentLanguage,selectedrow;     @implementation Localization

+(Localization *)sharedInstance
{
    static Localization *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[Localization alloc] init];
    });
    return sharedInstance;
}


+(NSString*) strSelectLanguage:(int)curLang{
    if(curLang==ARABIC){
        [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"ar", nil]forKey:@"AppleLanguages"];
    }
    else{
        [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil]forKey:@"AppleLanguages"];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
    currentLanguage=curLang;
    NSString *strLangSelect = [[[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"] objectAtIndex:0];
    return strLangSelect;
}

+(NSString*) languageSelectedStringForKey:(NSString*) key
{
    NSString *path;
    NSString *strSelectedLanguage = [[[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"] objectAtIndex:0];
    //When we check with iPhone,iPad device it shows "en-US".So we need to change it to "en"
    strSelectedLanguage = [strSelectedLanguage stringByReplacingOccurrencesOfString:@"en-US" withString:@"en"];
    if([strSelectedLanguage isEqualToString:[NSString stringWithFormat: @"en"]]){
        currentLanguage=ENGLISH;
        selectedrow=ENGLISH;
        path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
    }
    else{
        currentLanguage=ARABIC;
        selectedrow=ARABIC;
        path = [[NSBundle mainBundle] pathForResource:@"ar" ofType:@"lproj"];
    }
    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    NSString* str=[languageBundle localizedStringForKey:key value:@"" table:@"LocalizationArabic"];
    return str;
}

@end

然后是ViewController.h

#import <UIKit/UIKit.h>
#import "Localization.h"

@interface ViewController : UIViewController{
    Localization *localization;
}

@property (strong, nonatomic) IBOutlet UILabel *lblGo;

- (IBAction)actionChangeLanguageToArabic:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize lblGo;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    localization = [Localization sharedInstance];
    lblGo.text = [Localization languageSelectedStringForKey:@"Go"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)actionChangeLanguageToArabic:(id)sender {
    [Localization strSelectLanguage:ARABIC];
    lblGo.text = [Localization languageSelectedStringForKey:@"Go"];
}

@end

当我运行应用程序时,首先用英语显示

enter image description here

现在如果我点击按钮操作将语言更改为阿拉伯语

enter image description here