應用內動態更換語言

 

 

1.定義文件路徑宏:(全部文本都用宏)
#define CustomLocalizedString(key, comment) \
[[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userLanguage"]] ofType:@"lproj"]] localizedStringForKey:(key) value:@"" table:@"AppLanguage"]


2.根據alert選項取不一樣文件:
 if (IOS8_OR_LATER)
    {
        
        UIAlertController *languageAlert = [UIAlertController alertControllerWithTitle:@"" message:CustomLocalizedString(@"language_mode", nil) preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"language_cancelBtn", nil) style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *tcAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"login_language_item_TC", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant" forKey:@"userLanguage"];
            [self refreshHomeView];
        }];
        UIAlertAction *scAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"login_language_item_SC", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"userLanguage"];
            [self refreshHomeView];
        }];
        UIAlertAction *enAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"login_language_item_EN", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"userLanguage"];
            [self refreshHomeView];
        }];
        [languageAlert addAction:cancelAction];
        [languageAlert addAction:tcAction];
        [languageAlert addAction:scAction];
        [languageAlert addAction:enAction];
        [self presentViewController:languageAlert animated:YES completion:nil];
    }
    else
    {
        UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:CustomLocalizedString(@"language_mode", nil)
                                                          delegate:self
                                                          cancelButtonTitle:CustomLocalizedString(@"language_cancelBtn", nil)
                                                          destructiveButtonTitle:nil
                                                          otherButtonTitles:CustomLocalizedString(@"login_language_item_TC", nil),
                                                                            CustomLocalizedString(@"login_language_item_SC", nil),
                                                                            CustomLocalizedString(@"login_language_item_EN", nil),
                                                                            nil];
        [sheet showInView:self.view];
    
    }


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant" forKey:@"userLanguage"];
            [self refreshHomeView];
            break;
            
        case 1:
            [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"userLanguage"];
            [self refreshHomeView];
            break;
        case 2:
            [[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"userLanguage"];
            [self refreshHomeView];
            break;
        default:
            break;
    }
}


圖片其餘要區分語言的文件,相同原理。

 

相關文章
相關標籤/搜索