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 locales for relativeTime and formats from momentjs #560

Merged
merged 4 commits into from
Apr 26, 2019

Conversation

aadityataparia
Copy link
Contributor

@aadityataparia aadityataparia commented Apr 5, 2019

Inspired from #541

This PR imports relativeTime and formats from momentjs locales.

I have skipped the ones that uses function like https://github.com/moment/moment/blob/d0f0dd8f40be987593767a3dc5e27c45c6a3fa49/locale/he.js#L45-L50 to keep it simple

             hh : function (number) {
                if (number === 2) {
                    return 'שעתיים';
                }
                return number + ' שעות';
            }

I have also made locale tests independent, so that it tests all locales even when one of them fails, let me know if I need to revert that.

Locales with missing keys

[ 
  'ar-ly',
  'be',
  'br',
  'bs',
  'cv',
  'de-ch',
  'gl',
  'gom-latn',
  'is',
  'it-ch',
  'lb',
  'lv',
  'me',
  'mn',
  'mr',
  'sl',
  'tlh',
  'tzl'
]

@codecov-io
Copy link

codecov-io commented Apr 5, 2019

Codecov Report

Merging #560 into dev will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@         Coverage Diff         @@
##            dev   #560   +/-   ##
===================================
  Coverage   100%   100%           
===================================
  Files       153    153           
  Lines       957    957           
  Branches    127    127           
===================================
  Hits        957    957
Impacted Files Coverage Δ
src/locale/et.js 100% <ø> (ø) ⬆️
src/locale/he.js 100% <ø> (ø) ⬆️
src/locale/zh-tw.js 100% <ø> (ø) ⬆️
src/locale/zh-cn.js 100% <ø> (ø) ⬆️
src/locale/fa.js 100% <ø> (ø) ⬆️
src/locale/de.js 100% <ø> (ø) ⬆️
src/locale/sv.js 100% <ø> (ø) ⬆️
src/locale/it.js 100% <ø> (ø) ⬆️
src/locale/lt.js 100% <ø> (ø) ⬆️
src/locale/ro.js 100% <ø> (ø) ⬆️
... and 117 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 566873a...fd226f0. Read the comment docs.

@ghost
Copy link

ghost commented Apr 5, 2019

Looks great!
I just don't know how to do this. Can you share your script to me, please?

@aadityataparia
Copy link
Contributor Author

@xxyuk Sure: https://gist.github.com/aadityataparia/2972c69ae5a11bbbbe1f07bd69847a86

Also Tests are failing for:

  1. Using ar locale to check for fallback
  2. Locales which don't have all relativeTime keys (because they were functions)

@iamkun
Copy link
Owner

iamkun commented Apr 5, 2019

Perfect ~

@aadityataparia
Copy link
Contributor Author

aadityataparia commented Apr 5, 2019

@iamkun Does relativeTimePlugin supports functions? if yes maybe we can import simple functions.

@iamkun
Copy link
Owner

iamkun commented Apr 5, 2019

@aadityataparia Not yet. But we could do this.

@aadityataparia
Copy link
Contributor Author

aadityataparia commented Apr 5, 2019

@iamkun For now, should I remove relativeTime for locales which have function values?

@iamkun
Copy link
Owner

iamkun commented Apr 5, 2019

@iamkun For now, should I remove relativeTime for locales which have function values?

Yes, please. Still, we could add these later.

@aadityataparia
Copy link
Contributor Author

@iamkun Done, all tests are passing now.

.gitignore Outdated
@@ -13,7 +13,8 @@ package-lock.json
coverage

# build
/locale
/locale*
/moment*
Copy link
Owner

Choose a reason for hiding this comment

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

what's this used for?

Copy link
Contributor Author

@aadityataparia aadityataparia Apr 5, 2019

Choose a reason for hiding this comment

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

Ah, sorry! Removing

@@ -53,7 +53,10 @@ it('Uses English formats in other locales as default', () => {
const actualDate = dayjs(date)
const expectedDate = moment(date)
// todo: ar here isn't a good fix here
const arOldFormats = ar.formats
Copy link
Contributor Author

@aadityataparia aadityataparia Apr 5, 2019

Choose a reason for hiding this comment

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

Also, @iamkun Any alternatives?

Copy link
Owner

Choose a reason for hiding this comment

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

Never mind, I'll deal with this later.

@aadityataparia
Copy link
Contributor Author

@iamkun any timeline on when this will be merged? 🤔

@iamkun
Copy link
Owner

iamkun commented Apr 8, 2019

next release 😬

@iamkun iamkun merged commit 96b917e into iamkun:dev Apr 26, 2019
@aadityataparia aadityataparia deleted the aadi/relativeLocale branch April 26, 2019 04:09
@ghost
Copy link

ghost commented Apr 26, 2019

const fs = require('fs')
const path = require('path')

const momentLocaleFolder = path.join(__dirname, 'locale')
const dayjsLocaleFolder = path.join(__dirname, 'src/locale')
const saveFolder = path.join(__dirname, 'src/locale')
const Moment = require(path.join(__dirname, './moment'))

function objectToString(obj, prefix = '  ') {
  let string = '{'
  Object.keys(obj).forEach((p, i) => {
    const str = JSON.stringify(obj[p])
    if (str) string += `\n${prefix}${p}: ${str}${i < Object.keys(obj).length - 1 ? ',' : ''}`
  })
  string += '\n},'
  return string
}

fs.readdirSync(momentLocaleFolder).forEach((file) => {
  const filePath = path.join(momentLocaleFolder, file)
  require(filePath)
  const momentLocaleRelativeTime = Moment[file.replace('.js', '')].relativeTime
  const momentLocaleFormats = Moment[file.replace('.js', '')].longDateFormat
  delete momentLocaleRelativeTime.ss

  const dayjsFilePath = path.join(dayjsLocaleFolder, file)
  const dayjsFile = fs.readFileSync(dayjsFilePath, 'utf-8')
  const oldLocale = dayjsFile.split('const locale = ')[1].split('dayjs.locale')[0]

  let toBeAdded = ''
  if (oldLocale.indexOf('formats') < 0) {
    toBeAdded += `formats: ${objectToString(momentLocaleFormats)}\n  `
  }

  if (oldLocale.indexOf('relativeTime') < 0) {
    toBeAdded += `relativeTime: ${objectToString(momentLocaleRelativeTime)}\n  `
  }

  const start = oldLocale.substring(0, oldLocale.lastIndexOf('}'))
  const end = oldLocale.substring(oldLocale.lastIndexOf('}'), oldLocale.length)
  const localeToWrite = `${start}${toBeAdded ? ',' : ''}${toBeAdded}${end}`

  const saveLocalePath = path.join(saveFolder, file)
  fs.writeFileSync(saveLocalePath, `import dayjs from 'dayjs'
const locale = ${localeToWrite}dayjs.locale(locale, null, true)
export default locale
`)
})

// Copy momentjs locales folder to `locale` folder in dayjs root
// Create dummy `moment.js` with contents
// module.exports = {
//  defineLocale(name, val) {
//    this[name] = val
//  }
// }
// Change `saveFolder` if you want to test before overwriting original files in dayjs

aadityataparia 's solution looks great 😀

iamkun pushed a commit that referenced this pull request Apr 26, 2019
## [1.8.13](v1.8.12...v1.8.13) (2019-04-26)

### Bug Fixes

* Add missing relativeTime and formats for some locales ([#560](#560)) ([96b917e](96b917e))
* Add weekday (locale aware day of the week) plugin ([#569](#569)) ([9007cc5](9007cc5)), closes [#559](#559)
* Allow customizing "am" / "pm" strings with locale meridiem function ([#580](#580)) ([576e93e](576e93e)), closes [#578](#578)
* Fix `.add` day/week decimal rouding bug ([800f6c9](800f6c9))
* Fix `.diff` type definition error ([#565](#565)) ([c4921ae](c4921ae)), closes [#561](#561)
* Fix CustomParseFormat plugin bug ([#568](#568)) ([1f5a9db](1f5a9db)), closes [#555](#555)
* Fix relativeTime plugin Math.round bug ([40bea40](40bea40))
* skip square brackets in  buddhistEra, advancedFormat plugins ([#556](#556)) ([9279718](9279718)), closes [#554](#554)
* Update Indonesian locale([#574](#574)) ([0aa7143](0aa7143))
* Update locale month to support both array and function ([#581](#581)) ([b6599d3](b6599d3))
* Update LocalizedFormat plugin lowercase formats logic ([#557](#557)) ([d409304](d409304))
@iamkun
Copy link
Owner

iamkun commented Apr 26, 2019

🎉 This PR is included in version 1.8.13 🎉

The release is available on:

Your semantic-release bot 📦🚀

andrewhood125ruhuc added a commit to andrewhood125ruhuc/SidRH2 that referenced this pull request May 10, 2022
## [1.8.13](iamkun/dayjs@v1.8.12...v1.8.13) (2019-04-26)

### Bug Fixes

* Add missing relativeTime and formats for some locales ([#560](iamkun/dayjs#560)) ([96b917e](iamkun/dayjs@96b917e))
* Add weekday (locale aware day of the week) plugin ([#569](iamkun/dayjs#569)) ([9007cc5](iamkun/dayjs@9007cc5)), closes [#559](iamkun/dayjs#559)
* Allow customizing "am" / "pm" strings with locale meridiem function ([#580](iamkun/dayjs#580)) ([576e93e](iamkun/dayjs@576e93e)), closes [#578](iamkun/dayjs#578)
* Fix `.add` day/week decimal rouding bug ([800f6c9](iamkun/dayjs@800f6c9))
* Fix `.diff` type definition error ([#565](iamkun/dayjs#565)) ([c4921ae](iamkun/dayjs@c4921ae)), closes [#561](iamkun/dayjs#561)
* Fix CustomParseFormat plugin bug ([#568](iamkun/dayjs#568)) ([1f5a9db](iamkun/dayjs@1f5a9db)), closes [#555](iamkun/dayjs#555)
* Fix relativeTime plugin Math.round bug ([40bea40](iamkun/dayjs@40bea40))
* skip square brackets in  buddhistEra, advancedFormat plugins ([#556](iamkun/dayjs#556)) ([9279718](iamkun/dayjs@9279718)), closes [#554](iamkun/dayjs#554)
* Update Indonesian locale([#574](iamkun/dayjs#574)) ([0aa7143](iamkun/dayjs@0aa7143))
* Update locale month to support both array and function ([#581](iamkun/dayjs#581)) ([b6599d3](iamkun/dayjs@b6599d3))
* Update LocalizedFormat plugin lowercase formats logic ([#557](iamkun/dayjs#557)) ([d409304](iamkun/dayjs@d409304))
andrewhood125ruhuc added a commit to andrewhood125ruhuc/SidRH2 that referenced this pull request May 10, 2022
## [1.8.13](iamkun/dayjs@v1.8.12...v1.8.13) (2019-04-26)

### Bug Fixes

* Add missing relativeTime and formats for some locales ([#560](iamkun/dayjs#560)) ([96b917e](iamkun/dayjs@96b917e))
* Add weekday (locale aware day of the week) plugin ([#569](iamkun/dayjs#569)) ([9007cc5](iamkun/dayjs@9007cc5)), closes [#559](iamkun/dayjs#559)
* Allow customizing "am" / "pm" strings with locale meridiem function ([#580](iamkun/dayjs#580)) ([576e93e](iamkun/dayjs@576e93e)), closes [#578](iamkun/dayjs#578)
* Fix `.add` day/week decimal rouding bug ([800f6c9](iamkun/dayjs@800f6c9))
* Fix `.diff` type definition error ([#565](iamkun/dayjs#565)) ([c4921ae](iamkun/dayjs@c4921ae)), closes [#561](iamkun/dayjs#561)
* Fix CustomParseFormat plugin bug ([#568](iamkun/dayjs#568)) ([1f5a9db](iamkun/dayjs@1f5a9db)), closes [#555](iamkun/dayjs#555)
* Fix relativeTime plugin Math.round bug ([40bea40](iamkun/dayjs@40bea40))
* skip square brackets in  buddhistEra, advancedFormat plugins ([#556](iamkun/dayjs#556)) ([9279718](iamkun/dayjs@9279718)), closes [#554](iamkun/dayjs#554)
* Update Indonesian locale([#574](iamkun/dayjs#574)) ([0aa7143](iamkun/dayjs@0aa7143))
* Update locale month to support both array and function ([#581](iamkun/dayjs#581)) ([b6599d3](iamkun/dayjs@b6599d3))
* Update LocalizedFormat plugin lowercase formats logic ([#557](iamkun/dayjs#557)) ([d409304](iamkun/dayjs@d409304))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants