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


  مرحبا


会成为 :


  马哈巴


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


  康尼吉哈


会成为 :


  康尼吉哈


有什么简单的方法可以在obj c中做到这一点?

最佳答案

我创建了一个示例项目并尝试为您的问题找到解决方案,并且获得了很好的结果,它可以正常工作,我使用Go English语言并将其转换为阿拉伯语اذهب。

第一步:在阿拉伯语的本地化中添加阿拉伯语
信息(项目->信息->本地化)

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


ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

在波纹管本地化中,您现在可以看到+,然后单击+。单击后将显示弹出框,然后从中选择阿拉伯语

ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

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

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


ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

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


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

Click Localization


ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

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


ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

Now click Arabic and English checkboxes


ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

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

ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

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

ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

标题文件上方的名称是-LanguageHeader

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

ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

在进行编码之前,我想展示我的Storyboard设计

ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

最终代码如下

首先是NSObject类的Localization类

本地化

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

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

@end


本地化

#import“ Localization.h”
    int currentLanguage,selectedrow;
    @实现本地化

+(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


当我运行该应用程序时,它首先以英文显示

ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

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

ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?-LMLPHP

关于ios - 在iOS中将阿拉伯语单词转换为英语的简单方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38434218/

10-12 01:52