Showing posts with label NSLocale. Show all posts
Showing posts with label NSLocale. Show all posts

Saturday, May 11, 2013

NSLocale NSLocaleLanguageDirectionRightToLeft example ios


NSLocaleLanguageDirection - NSLocaleLanguageDirectionRightToLeft

These constants describe the text direction for a language. Used by the methodslineDirectionForLanguage: and characterDirectionForLanguage:.
enum {
   NSLocaleLanguageDirectionUnknown = kCFLocaleLanguageDirectionUnknown,
   NSLocaleLanguageDirectionLeftToRight = kCFLocaleLanguageDirectionLeftToRight,
   NSLocaleLanguageDirectionRightToLeft = kCFLocaleLanguageDirectionRightToLeft,
   NSLocaleLanguageDirectionTopToBottom = kCFLocaleLanguageDirectionTopToBottom,
   NSLocaleLanguageDirectionBottomToTop = kCFLocaleLanguageDirectionBottomToTop
};
typedef NSUInteger NSLocaleLanguageDirection;
Constants
NSLocaleLanguageDirectionUnknown
The direction of the language is unknown.
Available in iOS 4.0 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionLeftToRight
The language direction is from left to right.
Available in iOS 4.0 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionRightToLeft
The language direction is from right to left.
Available in iOS 4.0 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionTopToBottom
The language direction is from top to bottom.
Available in iOS 4.0 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionBottomToTop
The language direction is from bottom to top.
Available in iOS 4.0 and later.
Declared in NSLocale.h.



Example of [NSLocale NSLocaleLanguageDirectionRightToLeft]

static BOOL s_isRtl = NO;
+ initialize
{
    s_isRtl = [NSLocale characterDirectionForLanguage:[NSBundle mainBundle].preferredLocalizations[0]] == NSLocaleLanguageDirectionRightToLeft;
}

Example of [NSLocale NSLocaleLanguageDirectionRightToLeft]
+ (BOOL)isRtl
{
    static BOOL isRtl = NO;
    static BOOL isRtlFound = NO;
    if (!isRtlFound)
    { // This is "safe enough". Worst case is this code will be called twice in the app's lifecycle...
        isRtl = [NSLocale characterDirectionForLanguage:[NSBundle mainBundle].preferredLocalizations[0]] == NSLocaleLanguageDirectionRightToLeft;
        isRtlFound = YES;
    }
    return isRtl;
}
Example of [NSLocale NSLocaleLanguageDirectionRightToLeft]
- (BOOL)isRTL {
  return ([NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]] == NSLocaleLanguageDirectionRightToLeft);
}

-(void) changeLeftAllignment
{
    myTextView.textAlignment = (![self isRTL]]) ? UITextAlignmentLeft : UITextAlignmentRight;
}

-(void) changeRightAllignment
{
    myTextView.textAlignment = (![self isRTL]]) ? UITextAlignmentRight : UITextAlignmentLeft;
}

NSLocale localeIdentifier example ios


localeIdentifier

Returns the identifier for the receiver.
- (NSString *)localeIdentifier
Return Value of [NSLocale localeIdentifier]
The identifier for the receiver. This may not be the same string that the locale was created with, since NSLocale may canonicalize it.
Discussion
Equivalent to sending objectForKey: with key NSLocaleIdentifier.
Example of [NSLocale localeIdentifier]
 NSArray *arr = [NSLocale preferredLanguages];
for (NSString *lan in arr) {
    NSLog(@"%@: %@ %@",lan, [NSLocale canonicalLanguageIdentifierFromString:lan], [[[NSLocale alloc] initWithLocaleIdentifier:lan] displayNameForKey:NSLocaleIdentifier value:lan]);
}
Example of [NSLocale localeIdentifier]
NSLocale *locale = [NSLocale currentLocale];

NSString *language = [locale displayNameForKey:NSLocaleIdentifier 
                                         value:[locale localeIdentifier]];

Example of [NSLocale localeIdentifier]
- (NSLocale *) findLocaleByCurrencyCode:(NSString *)_currencyCode
{
        NSArray *locales = [NSLocale availableLocaleIdentifiers];
        NSLocale *locale = nil;
        NSString *localeId;

        for (localeId in locales) {
                locale = [[[NSLocale alloc] initWithLocaleIdentifier:localeId] autorelease];
                NSString *code = [locale objectForKey:NSLocaleCurrencyCode];
                if ([code isEqualToString:_currencyCode])
                        break;
                else
                        locale = nil;
        }

        /* For some codes that locale cannot be found, init it different way. */
        if (locale == nil) {
                NSDictionary *components = [NSDictionary dictionaryWithObject:_currencyCode
                                                                       forKey:NSLocaleCurrencyCode];

                localeId = [NSLocale localeIdentifierFromComponents:components];
                locale = [[NSLocale alloc] initWithLocaleIdentifier:localeId];
        }
        return locale;
}

NSLocale displayNameForKey value example ios


displayNameForKey value

Returns the display name for the given value.
- (NSString *)displayNameForKey:(id)key value:(id)value
Parameters
key
Specifies which of the locale property keys value is (see “Constants”),
value
A value for key.
Return Value of [NSLocale displayNameForKey value]
The display name for value.
Discussion
Not all locale property keys have values with display name values.
Example of [NSLocale displayNameForKey value]
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];

NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];

NSString *country = [usLocale displayNameForKey: NSLocaleCountryCode value: countryCode];
Example of [NSLocale displayNameForKey value]
NSLocale *french = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];
NSString *frenchName = [french displayNameForKey:NSLocaleIdentifier value:@"fr_FR"];

NSLocale *english = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
NSString *englishName = [english  displayNameForKey:NSLocaleIdentifier value:@"fr_FR"];

NSLog(@"French locale in French is called '%@'", frenchName);
NSLog(@"French locale in English is called '%@'", englishName);
Example of [NSLocale displayNameForKey value]
NSArray *languages = [NSLocale preferredLanguages];
for (NSString *language in languages) {
  NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:language] autorelease];
  NSLog(@"language code = %@, display name = %@, in language = %@",
        language,
        [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:language],
        [locale displayNameForKey:NSLocaleIdentifier value:language]);
}

NSLocale displayNameForKey example ios


displayNameForKey :value:

Returns the display name for the given value.
- (NSString *)displayNameForKey:(id)key value:(id)value
Parameters
key
Specifies which of the locale property keys value is (see “Constants”),
value
A value for key.
Return Value of [NSLocale displayNameForKey]
The display name for value.
Discussion
Not all locale property keys have values with display name values.
Example of [NSLocale displayNameForKey]
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];

NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];

NSString *country = [usLocale displayNameForKey: NSLocaleCountryCode value: countryCode];
Example of [NSLocale displayNameForKey]
NSLocale *french = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];
NSString *frenchName = [french displayNameForKey:NSLocaleIdentifier value:@"fr_FR"];

NSLocale *english = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
NSString *englishName = [english  displayNameForKey:NSLocaleIdentifier value:@"fr_FR"];

NSLog(@"French locale in French is called '%@'", frenchName);
NSLog(@"French locale in English is called '%@'", englishName);
Example of [NSLocale displayNameForKey]
NSArray *languages = [NSLocale preferredLanguages];
for (NSString *language in languages) {
  NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:language] autorelease];
  NSLog(@"language code = %@, display name = %@, in language = %@",
        language,
        [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:language],
        [locale displayNameForKey:NSLocaleIdentifier value:language]);
}