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

add basic support for hu and lt localizations #864

Merged
merged 5 commits into from Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/esm/locale/index.js
Expand Up @@ -8,6 +8,8 @@ export {default as eo} from './eo/index.js'
export {default as es} from './es/index.js'
export {default as fr} from './fr/index.js'
export {default as he} from './he/index.js'
export {default as hu} from './hu/index.js'
export {default as lt} from './lt/index.js'
export {default as nb} from './nb/index.js'
export {default as nl} from './nl/index.js'
export {default as ptBR} from './pt-BR/index.js'
Expand Down
135 changes: 135 additions & 0 deletions src/locale/hu/_lib/formatDistance/index.js
@@ -0,0 +1,135 @@
var formatDistanceLocale = {
lessThanXSeconds: {
one: translate,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a feeling that this could be improved somehow. formatDistance file doesn't has to have this exact structure

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, this is already merged 😀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have a feeling that this could be improved somehow. formatDistance file doesn't has to have this exact structure

@leshakoss could you please elaborate what do you mean by that? Guys could improve in following PRs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case formatDistanceLocale object is not really needed because the logic is handled by translate function below

other: translate
},

xSeconds: {
one: translate,
other: translate
},

halfAMinute: 'fél perce',

lessThanXMinutes: {
one: translate,
other: translate
},

xMinutes: {
one: translate,
other: translate
},

aboutXHours: {
one: translate,
other: translate
},

xHours: {
one: translate,
other: translate
},

xDays: {
one: translate,
other: translate
},

aboutXMonths: {
one: translate,
other: translate
},

xMonths: {
one: translate,
other: translate
},

aboutXYears: {
one: translate,
other: translate
},

xYears: {
one: translate,
other: translate
},

overXYears: {
one: translate,
other: translate
},

almostXYears: {
one: translate,
other: translate
}
}

var translations = {
'about': 'körülbelül',
'over': 'több mint',
'almost': 'majdnem',
'lessthan': 'kevesebb, mint'
}

function translate (number, addSuffix, key, isFuture) {
var num = number
switch (key) {
case 'xseconds_one':
return (isFuture || !addSuffix) ? 'néhány másodperc' : 'néhány másodperce'
case 'xseconds_other':
return num + ((isFuture || !addSuffix) ? ' másodperc' : ' másodperce')
case 'xminutes_one':
return 'egy' + (isFuture || !addSuffix ? ' perc' : ' perce')
case 'xminutes_other':
return num + ((isFuture || !addSuffix ? ' perc' : ' perce'))
case 'xhours_one':
return 'egy' + (isFuture || !addSuffix ? ' óra' : ' órája')
case 'xhours_other':
return num + ((isFuture || !addSuffix ? ' óra' : ' órája'))
case 'xdays_one':
return 'egy' + (isFuture || !addSuffix ? ' nap' : ' napja')
case 'xdays_other':
return num + ((isFuture || !addSuffix ? ' nap' : ' napja'))
case 'xmonths_one':
return 'egy' + (isFuture || !addSuffix ? ' hónap' : ' hónapja')
case 'xmonths_other':
return num + ((isFuture || !addSuffix ? ' hónap' : ' hónapja'))
case 'xyears_one':
return 'egy' + (isFuture || !addSuffix ? ' év' : ' éve')
case 'xyears_other':
return num + ((isFuture || !addSuffix ? ' év' : ' éve'))
}
return ''
}

export default function formatDistance (token, count, options) {
options = options || {}
var adverb = token.match(/about|over|almost|lessthan/i)
var unit = token.replace(adverb, '')

var result
if (typeof formatDistanceLocale[token] === 'string') {
result = formatDistanceLocale[token]
} else if (count === 1) {
result = formatDistanceLocale[token].one(count, options.addSuffix, unit.toLowerCase() + '_one', options.comparison > 0)
} else {
result = formatDistanceLocale[token].other(count, options.addSuffix, unit.toLowerCase() + '_other', options.comparison > 0)
}

if (adverb) {
result = translations[adverb[0].toLowerCase()] + ' ' + result
}

if (options.addSuffix) {
if (options.comparison > 0) {
return result + ' múlva'
} else {
return result + ' ezelőtt'
}
}

return result
}
219 changes: 219 additions & 0 deletions src/locale/hu/_lib/formatDistance/test.js
@@ -0,0 +1,219 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import formatDistance from '.'

describe('hu locale > formatDistance', function () {
describe('lessThanXSeconds', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('lessThanXSeconds', 1) === 'kevesebb, mint néhány másodperc')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('lessThanXSeconds', 2) === 'kevesebb, mint 2 másodperc')
})
})
})

describe('xSeconds', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xSeconds', 1) === 'néhány másodperc')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xSeconds', 2) === '2 másodperc')
})
})
})

describe('halfAMinute', function () {
it('returns a proper string', function () {
assert(formatDistance('halfAMinute') === 'fél perce')
})

it('ignores the second argument', function () {
assert(formatDistance('halfAMinute', 123) === 'fél perce')
})
})

describe('lessThanXMinutes', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('lessThanXMinutes', 1) === 'kevesebb, mint egy perc')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('lessThanXMinutes', 2) === 'kevesebb, mint 2 perc')
})
})
})

describe('xMinutes', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xMinutes', 1) === 'egy perc')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xMinutes', 2) === '2 perc')
})
})
})

describe('aboutXHours', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('aboutXHours', 1) === 'körülbelül egy óra')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('aboutXHours', 2) === 'körülbelül 2 óra')
})
})
})

describe('xHours', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xHours', 1) === 'egy óra')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xHours', 2) === '2 óra')
})
})
})

describe('xDays', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xDays', 1) === 'egy nap')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xDays', 2) === '2 nap')
})
})
})

describe('aboutXMonths', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('aboutXMonths', 1) === 'körülbelül egy hónap')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('aboutXMonths', 2) === 'körülbelül 2 hónap')
})
})
})

describe('xMonths', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xMonths', 1) === 'egy hónap')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xMonths', 2) === '2 hónap')
})
})
})

describe('aboutXYears', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('aboutXYears', 1) === 'körülbelül egy év')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('aboutXYears', 2) === 'körülbelül 2 év')
})
})
})

describe('xYears', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xYears', 1) === 'egy év')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('xYears', 2) === '2 év')
})
})
})

describe('overXYears', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('overXYears', 1) === 'több mint egy év')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('overXYears', 2) === 'több mint 2 év')
})
})
})

describe('almostXYears', function () {
context('when the count equals 1', function () {
it('returns a proper string', function () {
assert(formatDistance('almostXYears', 1) === 'majdnem egy év')
})
})

context('when the count is more than 1', function () {
it('returns a proper string', function () {
assert(formatDistance('almostXYears', 2) === 'majdnem 2 év')
})
})
})

context('with a past suffix', function () {
it('adds `ezelőtt` to a string', function () {
var result = formatDistance('aboutXYears', 1, {
addSuffix: true,
comparison: -1
})
assert(result === 'körülbelül egy éve ezelőtt')
})
})

context('with a future suffix', function () {
it('adds `múlva` to a string', function () {
var result = formatDistance('halfAMinute', null, {
addSuffix: true,
comparison: 1
})
assert(result === 'fél perce múlva')
})
})
})