iOS manual language selection

0 意見

The original approach is following the  stack overflow article manual language selection in an iOS-App (iPhone and iPad) by Hubert Schölnast


Header file LocalizeHelper.h

// macro
// 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)]


#import 

@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



Implementation file : 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



How to use. first, prepare the two localizable.string file

    NSString *selectedLanguage = @"zh-Hant";
    LocalizationSetLanguage(selectedLanguage);
    
    NSLog(@"%@",LocalizedString(@"TITLE"));
    
    selectedLanguage = @"en";
    LocalizationSetLanguage(selectedLanguage);
    
    NSLog(@"%@",LocalizedString(@"TITLE"));

iOS how to manual language to your app

0 意見

There are approach to localize an iPhone app already. In this article we are going to learn how to get localize string file , and then transfer to NSDictionary class. By NSDictionary object, we can get localize string by inputing dedicated key.


NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable" ofType:@"strings" inDirectory:nil forLocalization:@"zh-Hant"];
    
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSString *twTranslation = [dict objectForKey:@"TITLE"];
    
NSLog(@"%@", twTranslation);

iOS get current date and time

0 意見

It is simple to get current date and time, Apple Developer have a Class named NSDate, bravo!! Besides, function dateFormat can format the date and time, awesome. Finally, using the setTimeZone function to set timezone.

NSDate *now = [NSDate date];
NSDateFormatter *currentTime = [[NSDateFormatter alloc] init];

currentTime.dateFormat = @"yyyy:mm:dd:hh:mm:ss";
[currentTime setTimeZone:[NSTimeZone systemTimeZone]];

NSLog(@"time:%@",[currentTime stringFromDate:now]);

iOS get current language

0 意見

how to get current lanugage in your iOS,  following code is the simple solution.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
    
NSLog(@"Current Locale: %@", [[NSLocale currentLocale] localeIdentifier]);
NSLog(@"Current language: %@", currentLanguage);

detail please see apple developer NSUserDefaults

Android Calendar to get current date and time

0 意見

In applications, we may need the current system date and time. Android developer have a Class named Calendar, let's go see it.


Calendar now = Calendar.getInstance();
  
int year = now.get(Calendar.YEAR)
int month= now.get(Calendar.MONTH);
int day  = now.get(Calendar.DAY_OF_MONTH);

int hour = now.get(Calendar.HOUR);
int min  = now.get(Calendar.MINUTE);
.....

detail please see android developer Calendar

Android action bar to set title of window

0 意見

The action bar is a window feature to identifies the application. We can easily use the setTitle API to set title.

ActionBar ab = getActionBar();
ab.setTitle(R.string.title);
detail please see android developer Action Bar

iOS set Keyboard Type

0 意見

It is simple to set keyboard type for your TextField
First of all, let's get enum


typedef enum {
   UIKeyboardTypeDefault,
   UIKeyboardTypeASCIICapable,
   UIKeyboardTypeNumbersAndPunctuation,
   UIKeyboardTypeURL,
   UIKeyboardTypeNumberPad,
   UIKeyboardTypePhonePad,
   UIKeyboardTypeNamePhonePad,
   UIKeyboardTypeEmailAddress,
   UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable
} UIKeyboardType;

// set keyboard type to UITextField, Done.
[textField setKeyboardType:UIKeyboardTypeDecimalPad];
here is the detail from Share Our Ideas.