How to display localized month names?

The Problem and the Solution

While the problem might seem trivial, those without experience in Objective-C could spend a significant amount of time searching for the appropriate objects and methods. To address this, I’m presenting a ready-made solution:

The Kalendarz Class

The Kalendarz class takes an NSDictionary object as input and returns the corresponding month name as an NSString.

Kalendarz.h

Objective-C
@interface Kalendarz : NSObject {
}
-(NSString *)jakiMiesiac:(NSDictionary *)rokMiesiacDzien;
@end
  • The jakiMiesiac: method takes an NSDictionary object as input and returns the corresponding month name as an NSString.

Kalendarz.m

Objective-C
@implementation Kalendarz

-(NSString *)jakiMiesiac:(NSDictionary *)rokMiesiacDzien;
{
    NSInteger miesiacNumber=[[rokMiesiacDzien valueForKey:@"month"] intValue]; // Extract month number from the dictionary
    return [[self dateCreate:miesiacNumber] autorelease];
}

-(NSString *)dateCreate:(NSInteger)month
{
    // Create a date formatter to format the month name
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMMM"]; // Set the format to display the full month name

    // Create a Gregorian calendar
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    // Create date components with the specified month number
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:month];

    // Create a date object using the Gregorian calendar and date components
    NSDate *yearDate = [gregorian dateFromComponents:comps];

    // Convert the date object to a string using the date formatter
    NSString *miesiacName=[dateFormat stringFromDate:yearDate];

    // Release unused objects
    [comps release];
    [gregorian release];
    [dateFormat release];

    // Return the month name
    return [miesiacName autorelease];
}

@end
  • The jakiMiesiac: method:

    1. Extracts the month number from the provided NSDictionary using valueForKey:@"month".
    2. Calls the dateCreate: method to convert the month number to a month name.
    3. Returns the month name as an NSString.
  • The dateCreate: method:

    1. Creates an NSDateFormatter object to format the month name.
    2. Sets the date format to “MMMM” to display the full month name.
    3. Creates an NSCalendar object using the Gregorian calendar.
    4. Creates NSDateComponents with the specified month number.
    5. Creates a NSDate object using the Gregorian calendar and date components.
    6. Converts the NSDate object to a string using the date formatter.
    7. Releases unused objects: NSDateComponents, NSCalendar, NSDateFormatter.
    8. Returns the month name as an NSString.

Example Usage

Objective-C
Kalendarz *newKalendarz=[[Kalendarz alloc] init]; // Create a Kalendarz object

// Create a dictionary with the month number (assuming 'miesiac' is an integer variable)
NSDictionary *rokMiesiacDzien=[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:miesiac+1] forKey:@"month"];

// Retrieve the month name using the Kalendarz object and method
NSString *miesiacName=[[NSString alloc] initWithString:[[newKalendarz jakiMiesiac:rokMiesiacDzien] retain]];

// ... Use the month name as needed

// Release unused objects
[newKalendarz release];
[miesiacName release];

This code snippet demonstrates how to use the Kalendarz class to retrieve the month name based on a provided month number. It provides a convenient and reusable way to handle month name conversion in Objective-C applications.