Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Calendar localizable #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Examples/NativeCal/Classes/NativeCalAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application
* If your application requires an arbitrary starting date, use -[KalViewController initWithSelectedDate:]
* instead of -[KalViewController init].
*/
kal = [[KalViewController alloc] init];
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"zh-HK"] autorelease];
kal = [[KalViewController alloc] initWithLocale:locale];
kal.title = @"NativeCal";

/*
Expand Down
4 changes: 2 additions & 2 deletions src/KalGridView.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ - (id)initWithFrame:(CGRect)frame logic:(KalLogic *)theLogic delegate:(id<KalVie
delegate = theDelegate;

CGRect monthRect = CGRectMake(0.f, 0.f, frame.size.width, frame.size.height);
frontMonthView = [[KalMonthView alloc] initWithFrame:monthRect];
backMonthView = [[KalMonthView alloc] initWithFrame:monthRect];
frontMonthView = [[KalMonthView alloc] initWithFrame:monthRect logic:theLogic];
backMonthView = [[KalMonthView alloc] initWithFrame:monthRect logic:theLogic];
backMonthView.hidden = YES;
[self addSubview:backMonthView];
[self addSubview:frontMonthView];
Expand Down
2 changes: 2 additions & 0 deletions src/KalLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
NSDateFormatter *monthAndYearFormatter;
}

@property (nonatomic, retain, readonly) NSLocale *locale;
@property (nonatomic, retain) NSDate *baseDate; // The first day of the currently selected month
@property (nonatomic, retain, readonly) NSDate *fromDate; // The date corresponding to the tile in the upper-left corner of the currently selected month
@property (nonatomic, retain, readonly) NSDate *toDate; // The date corresponding to the tile in the bottom-right corner of the currently selected month
Expand All @@ -40,6 +41,7 @@
@property (nonatomic, readonly) NSString *selectedMonthNameAndYear; // localized (e.g. "September 2010" for USA locale)

- (id)initForDate:(NSDate *)date; // designated initializer.
- (id)initForDate:(NSDate *)date locale:(NSLocale *)theLocale;

- (void)retreatToPreviousMonth;
- (void)advanceToFollowingMonth;
Expand Down
25 changes: 17 additions & 8 deletions src/KalLogic.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ - (void)recalculateVisibleDays;
- (NSUInteger)numberOfDaysInPreviousPartialWeek;
- (NSUInteger)numberOfDaysInFollowingPartialWeek;

@property (nonatomic, retain) NSLocale *locale;
@property (nonatomic, retain) NSDate *fromDate;
@property (nonatomic, retain) NSDate *toDate;
@property (nonatomic, retain) NSArray *daysInSelectedMonth;
Expand All @@ -23,7 +24,7 @@ - (NSUInteger)numberOfDaysInFollowingPartialWeek;

@implementation KalLogic

@synthesize baseDate, fromDate, toDate, daysInSelectedMonth, daysInFinalWeekOfPreviousMonth, daysInFirstWeekOfFollowingMonth;
@synthesize locale, baseDate, fromDate, toDate, daysInSelectedMonth, daysInFinalWeekOfPreviousMonth, daysInFirstWeekOfFollowingMonth;

+ (NSSet *)keyPathsForValuesAffectingSelectedMonthNameAndYear
{
Expand All @@ -32,12 +33,20 @@ + (NSSet *)keyPathsForValuesAffectingSelectedMonthNameAndYear

- (id)initForDate:(NSDate *)date
{
if ((self = [super init])) {
monthAndYearFormatter = [[NSDateFormatter alloc] init];
[monthAndYearFormatter setDateFormat:@"LLLL yyyy"];
[self moveToMonthForDate:date];
}
return self;
return [self initForDate:date locale:nil];
}

- (id)initForDate:(NSDate *)date locale:(NSLocale *)theLocale
{
if ((self = [super init])) {
self.locale = theLocale;
NSString *format = [NSDateFormatter dateFormatFromTemplate:@"MMMM yyyy" options:0 locale:theLocale];
monthAndYearFormatter = [[NSDateFormatter alloc] init];
[monthAndYearFormatter setDateFormat:format];
[monthAndYearFormatter setLocale:theLocale];
[self moveToMonthForDate:date];
}
return self;
}

- (id)init
Expand All @@ -63,7 +72,7 @@ - (void)advanceToFollowingMonth

- (NSString *)selectedMonthNameAndYear;
{
return [monthAndYearFormatter stringFromDate:self.baseDate];
return [monthAndYearFormatter stringFromDate:self.baseDate];
}

#pragma mark Low-level implementation details
Expand Down
4 changes: 4 additions & 0 deletions src/KalMonthView.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#import <UIKit/UIKit.h>

#import "KalLogic.h"

@class KalTileView, KalDate;

@interface KalMonthView : UIView
Expand All @@ -16,6 +18,8 @@
@property (nonatomic) NSUInteger numWeeks;

- (id)initWithFrame:(CGRect)rect; // designated initializer
- (id)initWithFrame:(CGRect)rect logic:(KalLogic *)logic;

- (void)showDates:(NSArray *)mainDates leadingAdjacentDates:(NSArray *)leadingAdjacentDates trailingAdjacentDates:(NSArray *)trailingAdjacentDates;
- (KalTileView *)firstTileOfMonth;
- (KalTileView *)tileForDate:(KalDate *)date;
Expand Down
33 changes: 20 additions & 13 deletions src/KalMonthView.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,28 @@ @implementation KalMonthView

@synthesize numWeeks;

- (id)initWithFrame:(CGRect)frame
- (id)initWithFrame:(CGRect)rect logic:(KalLogic *)logic
{
if ((self = [super initWithFrame:frame])) {
tileAccessibilityFormatter = [[NSDateFormatter alloc] init];
[tileAccessibilityFormatter setDateFormat:@"EEEE, MMMM d"];
self.opaque = NO;
self.clipsToBounds = YES;
for (int i=0; i<6; i++) {
for (int j=0; j<7; j++) {
CGRect r = CGRectMake(j*kTileSize.width, i*kTileSize.height, kTileSize.width, kTileSize.height);
[self addSubview:[[[KalTileView alloc] initWithFrame:r] autorelease]];
}
if ((self = [super initWithFrame:rect])) {
NSString *format = [NSDateFormatter dateFormatFromTemplate:@"EEEE, MMM d" options:0 locale:logic.locale];
tileAccessibilityFormatter = [[NSDateFormatter alloc] init];
[tileAccessibilityFormatter setDateFormat:format];
[tileAccessibilityFormatter setLocale:logic.locale];
self.opaque = NO;
self.clipsToBounds = YES;
for (int i=0; i<6; i++) {
for (int j=0; j<7; j++) {
CGRect r = CGRectMake(j*kTileSize.width, i*kTileSize.height, kTileSize.width, kTileSize.height);
[self addSubview:[[[KalTileView alloc] initWithFrame:r] autorelease]];
}
}
}
}
return self;
return self;
}

- (id)initWithFrame:(CGRect)frame
{
return [self initWithFrame:frame logic:nil];
}

- (void)showDates:(NSArray *)mainDates leadingAdjacentDates:(NSArray *)leadingAdjacentDates trailingAdjacentDates:(NSArray *)trailingAdjacentDates
Expand Down
7 changes: 5 additions & 2 deletions src/KalView.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ - (void)addSubviewsToHeaderView:(UIView *)headerView
[nextMonthButton release];

// Add column labels for each weekday (adjusting based on the current locale's first weekday)
NSArray *weekdayNames = [[[[NSDateFormatter alloc] init] autorelease] shortWeekdaySymbols];
NSArray *fullWeekdayNames = [[[[NSDateFormatter alloc] init] autorelease] standaloneWeekdaySymbols];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:logic.locale];
NSArray *weekdayNames = [df shortWeekdaySymbols];
NSArray *fullWeekdayNames = [df standaloneWeekdaySymbols];
[df release];
NSUInteger firstWeekday = [[NSCalendar currentCalendar] firstWeekday];
NSUInteger i = firstWeekday - 1;
for (CGFloat xOffset = 0.f; xOffset < headerView.width; xOffset += 46.f, i = (i+1)%7) {
Expand Down
4 changes: 4 additions & 0 deletions src/KalViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
@property (nonatomic, retain, readonly) NSDate *selectedDate;

- (id)initWithSelectedDate:(NSDate *)selectedDate; // designated initializer. When the calendar is first displayed to the user, the month that contains 'selectedDate' will be shown and the corresponding tile for 'selectedDate' will be automatically selected.

- (id)initWithSelectedDate:(NSDate *)date locale:(NSLocale *)locale;
- (id)initWithLocale:(NSLocale *)locale;

- (void)reloadData; // If you change the KalDataSource after the KalViewController has already been displayed to the user, you must call this method in order for the view to reflect the new data.
- (void)showAndSelectDate:(NSDate *)date; // Updates the state of the calendar to display the specified date's month and selects the tile for that date.

Expand Down
26 changes: 18 additions & 8 deletions src/KalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,26 @@ @implementation KalViewController

@synthesize dataSource, delegate, initialDate, selectedDate;

- (id)initWithSelectedDate:(NSDate *)date locale:(NSLocale *)locale
{
if ((self = [super init])) {
logic = [[KalLogic alloc] initForDate:date locale:locale];
self.initialDate = date;
self.selectedDate = date;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(significantTimeChangeOccurred) name:UIApplicationSignificantTimeChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:KalDataSourceChangedNotification object:nil];
}
return self;
}

- (id)initWithSelectedDate:(NSDate *)date
{
if ((self = [super init])) {
logic = [[KalLogic alloc] initForDate:date];
self.initialDate = date;
self.selectedDate = date;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(significantTimeChangeOccurred) name:UIApplicationSignificantTimeChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:KalDataSourceChangedNotification object:nil];
}
return self;
return [self initWithSelectedDate:date locale:nil];
}

- (id)initWithLocale:(NSLocale *)locale
{
return [self initWithSelectedDate:[NSDate date] locale:locale];
}

- (id)init
Expand Down