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 aliases #682

Closed
muuvmuuv opened this issue Sep 25, 2019 · 3 comments
Closed

Add aliases #682

muuvmuuv opened this issue Sep 25, 2019 · 3 comments

Comments

@muuvmuuv
Copy link

Describe the feature

I want to set dayjs locale with window.navigator.language. The problem is, that in my case, window.navigator.language returns de-DE which is not a valid format. It would be great if you could add aliases.

Here is a SO question with a list of valid locales, but I guess there must be something on the internet that browsers use as reference: https://stackoverflow.com/questions/3191664/list-of-all-locales-and-their-short-codes

@iamkun
Copy link
Owner

iamkun commented Sep 27, 2019

maybe you can add your own helper function to fix this at the moment. like

localeHelper = () => {}  //de-DE returns de 

@muuvmuuv
Copy link
Author

This works for the moment, thanks.

For anyone looking for something like that.:

const dayjs = require('dayjs')
const UTC = require('dayjs/plugin/utc')
const LocalizedFormat = require('dayjs/plugin/localizedFormat')
const { isDev } = require('../environment.js')

const availableLanguages = ['en', 'de']

// include locale for each available language
require('dayjs/locale/de')

const localeHelper = lang => {
  const locale = availableLanguages.find(locale => {
    if (locale === lang) {
      return true
    }
    const regex = new RegExp(`${locale}-.*`)
    return regex.test(lang)
  })
  return locale ? locale : availableLanguages[0]
}

module.exports = () => {
  const lang = window.navigator.language
  const locale = localeHelper(lang)

  dayjs.extend(LocalizedFormat)
  dayjs.extend(UTC)
  dayjs.locale(locale)

  if (isDev) {
    console.log('Preferred language:', lang, locale)
  }
}

@iamkun
Copy link
Owner

iamkun commented Nov 6, 2019

I'll close this issue since it's been a while since it's been opened. Feel free to reopen if you have updates on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants