diff --git a/src/locale/br.js b/src/locale/br.js index bbd242c7..17e4ee50 100644 --- a/src/locale/br.js +++ b/src/locale/br.js @@ -1,6 +1,47 @@ // Breton [br] import dayjs from 'dayjs' +function lastNumber(number) { + if (number > 9) { + return lastNumber(number % 10) + } + return number +} +function softMutation(text) { + const mutationTable = { + m: 'v', + b: 'v', + d: 'z' + } + return mutationTable[text.charAt(0)] + text.substring(1) +} +function mutation(text, number) { + if (number === 2) { + return softMutation(text) + } + return text +} +function relativeTimeWithMutation(number, withoutSuffix, key) { + const format = { + mm: 'munutenn', + MM: 'miz', + dd: 'devezh' + } + return `${number} ${mutation(format[key], number)}` +} +function specialMutationForYears(number) { + switch (lastNumber(number)) { + case 1: + case 3: + case 4: + case 5: + case 9: + return `${number} bloaz` + default: + return `${number} vloaz` + } +} + const locale = { name: 'br', weekdays: 'Sul_Lun_Meurzh_Mercʼher_Yaou_Gwener_Sadorn'.split('_'), @@ -18,6 +59,21 @@ const locale = { LLL: 'D [a viz] MMMM YYYY h[e]mm A', LLLL: 'dddd, D [a viz] MMMM YYYY h[e]mm A' }, + relativeTime: { + future: 'a-benn %s', + past: '%s ʼzo', + s: 'un nebeud segondennoù', + m: 'ur vunutenn', + mm: relativeTimeWithMutation, + h: 'un eur', + hh: '%d eur', + d: 'un devezh', + dd: relativeTimeWithMutation, + M: 'ur miz', + MM: relativeTimeWithMutation, + y: 'ur bloaz', + yy: specialMutationForYears + }, meridiem: hour => (hour < 12 ? 'a.m.' : 'g.m.') // a-raok merenn | goude merenn } diff --git a/test/locale/br.test.js b/test/locale/br.test.js new file mode 100644 index 00000000..1aa106a4 --- /dev/null +++ b/test/locale/br.test.js @@ -0,0 +1,54 @@ +import moment from 'moment' +import MockDate from 'mockdate' +import dayjs from '../../src' +import relativeTime from '../../src/plugin/relativeTime' +import '../../src/locale/br' + +dayjs.extend(relativeTime) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('Format Month with locale function', () => { + for (let i = 0; i <= 7; i += 1) { + const dayjsBR = dayjs().locale('br').add(i, 'day') + const momentBR = moment().locale('br').add(i, 'day') + const testFormat1 = 'DD MMMM YYYY MMM' + const testFormat2 = 'MMMM' + const testFormat3 = 'MMM' + expect(dayjsBR.format(testFormat1)).toEqual(momentBR.format(testFormat1)) + expect(dayjsBR.format(testFormat2)).toEqual(momentBR.format(testFormat2)) + expect(dayjsBR.format(testFormat3)).toEqual(momentBR.format(testFormat3)) + } +}) + +it('RelativeTime: Time from X', () => { + const T = [ + [44.4, 'second'], // a few seconds + [89.5, 'second'], // a minute + [130, 'second'], // two minutes + [43, 'minute'], // 44 minutes + [1, 'hour'], // 1 hour + [21, 'hour'], // 21 hours + [2, 'day'], // 2 days + [25, 'day'], // 25 days + [2, 'month'], // 2 months + [10, 'month'], // 10 months + [18, 'month'], // 2 years + [15, 'year'] // 15 years + ] + + T.forEach((t) => { + dayjs.locale('br') + moment.locale('br') + expect(dayjs().from(dayjs().add(t[0], t[1]))) + .toBe(moment().from(moment().add(t[0], t[1]))) + expect(dayjs().from(dayjs().add(t[0], t[1]), true)) + .toBe(moment().from(moment().add(t[0], t[1]), true)) + }) +})