1

我正在使用 pod FSCalendar(https://github.com/WenchaoD/FSCalendar)并对其进行本地化,我使用:

 calendar.locale = Locale(identifier: String) 

但问题是翻译月份的最后几个字母在语法上听起来不太好。(就像“2016 年 12 月”而不是“2016 年 12 月”通缉语言)在这个豆荚里回答一个月?据我了解,我可以这样做

 if monthLabel.text.hasPrefix("") {
 monthLabel.text = //change to normal

}
4

5 回答 5

2

Swift 3 中的快速解决方案

首先继承 FSCalendarDelegate 然后添加这个...

func calendarCurrentPageDidChange(_ calendar: FSCalendar) {

    let currentMonth = calendar.month(of: calendar.currentPage)
    print("this is the current Month \(currentMonth)")
}

每次您每月滚动日历时,这个“currentMonth”都会改变。

于 2017-08-23T05:37:55.283 回答
2

对于 FSCalendarHeaderView.m 更改函数中的俄语语言环境

  • (void)configureCell:(FSCalendarHeaderCell *)cell atIndexPath:(NSIndexPath *)indexPath

为了这

- (void)configureCell:(FSCalendarHeaderCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    FSCalendarAppearance *appearance = self.calendar.appearance;
    cell.titleLabel.font = appearance.headerTitleFont;
    cell.titleLabel.textColor = appearance.headerTitleColor;
    _calendar.formatter.dateFormat = appearance.headerDateFormat;
    BOOL usesUpperCase = (appearance.caseOptions & 15) == FSCalendarCaseOptionsHeaderUsesUpperCase;
    NSString *text = nil;
    switch (self.calendar.transitionCoordinator.representingScope) {
        case FSCalendarScopeMonth: {
            if (_scrollDirection == UICollectionViewScrollDirectionHorizontal) {
                // 多出的两项需要制空
                if ((indexPath.item == 0 || indexPath.item == [self.collectionView numberOfItemsInSection:0] - 1)) {
                    text = nil;
                } else {
                    NSDate *date = [self.calendar.gregorian dateByAddingUnit:NSCalendarUnitMonth value:indexPath.item-1 toDate:self.calendar.minimumDate options:0];
                    text = [_calendar.formatter stringFromDate:date];
                }
            } else {
                NSDate *date = [self.calendar.gregorian dateByAddingUnit:NSCalendarUnitMonth value:indexPath.item toDate:self.calendar.minimumDate options:0];
                text = [_calendar.formatter stringFromDate:date];
            }
            break;
        }
        case FSCalendarScopeWeek: {
            if ((indexPath.item == 0 || indexPath.item == [self.collectionView numberOfItemsInSection:0] - 1)) {
                text = nil;
            } else {
                NSDate *firstPage = [self.calendar.gregorian fs_middleDayOfWeek:self.calendar.minimumDate];
                NSDate *date = [self.calendar.gregorian dateByAddingUnit:NSCalendarUnitWeekOfYear value:indexPath.item-1 toDate:firstPage options:0];
                text = [_calendar.formatter stringFromDate:date];
            }
            break;
        }
        default: {
            break;
        }
    }

    NSArray* foo = [text componentsSeparatedByString: @" "];
    NSString* firstBit = [foo objectAtIndex: 0];

    if ([firstBit isEqualToString:@"января"]) {
        firstBit = @"январь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"февраля"]) {
        firstBit = @"февраль ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"марта"]) {
        firstBit = @"март ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"апреля"]) {
        firstBit = @"апрель ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"мая"]) {
        firstBit = @"май ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"июня"]) {
        firstBit = @"июнь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"июля"]) {
        firstBit = @"июль ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"августа"]) {
        firstBit = @"август ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"сентября"]) {
        firstBit = @"сентябрь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"октября"]) {
        firstBit = @"октябрь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"ноября"]) {
        firstBit = @"ноябрь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }
    if ([firstBit isEqualToString:@"декабря"]) {
        firstBit = @"декабрь ";
        text = [firstBit stringByAppendingString:[foo objectAtIndex: 1]];
    }

    text = usesUpperCase ? text.uppercaseString : text;
    cell.titleLabel.text = text;
    [cell setNeedsLayout];
}
于 2017-04-27T16:23:27.610 回答
1

是的,FSCalendar 有错误的实现——它是……。喜欢使用 locale.calendar.monthSymbols 而不是 locale.calendar.standaloneMonthSymbols

快速解决方案是修复文件 FSCalendarHeaderView.m 中的 FSCalendar 代码

(void)configureCell:(FSCalendarHeaderCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    case FSCalendarScopeMonth: {
        //...
        //...
        text = [_calendar.formatter stringFromDate:date]; //**1
    }
}

**1 必须改成 sth。喜欢

NSDateComponents *cc = [self.calendar.gregorian components:(NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:date];

text = [NSString stringWithFormat:@"%@ %ld", _calendar.formatter.standaloneMonthSymbols[cc.month - 1], (long)cc.year];
于 2017-02-24T18:45:00.303 回答
0

您可以使用以下代码轻松且完美地做到这一点:

//objc
self.calendar.appearence.headerDateFormat = @"LLLL yyyy";

//swift
self.calendar.appearence.headerDateFormat = "LLLL yyyy"

要了解有关日期格式的更多信息,请使用 http://userguide.icu-project.org/formatparse/datetime中的表格

于 2017-12-28T10:43:23.250 回答
-1
var locale = NSLocale(localeIdentifier: "ru_RU")
calendar.locale = locale as Locale
于 2018-10-03T20:01:20.610 回答