-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Locales? #36
Comments
update: check our doc here
|
sorry, i don't speak ES5/6 😅 maybe stick to a moment.js approach for now? |
😅 ok, here is the compiled one in browser <script src="https://unpkg.com/dayjs"></script>
<script>
// French locales
dayjs.setWeekName(['dimanche', 'lundi', 'mardi', ...])
// return locales [dimanche]
dayjs().format('[dddd]')
</script> |
hmm, alright |
This is much like But do we really need an API like this? I mean perhaps some one want to return a string "4 years", another one want "4 years ago", and another one want something else(number 4 maybe). And maybe it's hard to satisfied them all. How do you think? |
How about going for something like this for handling language overrides? I think it looks pretty sweet.
Alternatively, for including an entire language file
|
I just came here from hn. I already had proposal for date-fns to use Intl.DateTimeFormat for most cases, so I can only suggest same here. :) Good job so far. |
and what are we going to do, for babelized js? You're going to use babel, right? |
I like b44rd approach. Just pass an object of key-value strings and let dayjs initialize with it, by overriding the default strings with the provided ones. |
I'll start working on a PR for the approach outlined by @b44rd , unless someone has an objection. |
@schiem I was trying something already :D But maybe we can finish it together. Actually, I'm considering two approaches, since the library actually limit the instantiation to its own module, and that could lead to performance issues when applying new strings. I'll show my progress as soon as I get home, after work |
I think a big mistake moment did is making the configured locale global. This is a big issue for Node.js server side where many requests might be processed at the same time. Having a API returning a new dayjs instance with the localization would be better: import dayjs from 'dayjs';
import withLocale from 'dayjs/locale';
import frenchConf from 'dayjs/locale/fr-FR';
const dayjsFR = withLocale(dayjs, { weekdays: ['Dimanche', 'Lundi', ...etc] });
const dayjsFR2 = withLocale(dayjs, frenchConf); |
@SBoudrias you could store different instances per language with this approach #77
Pinging @schiem 😃 |
That looks good to me! That's really similar to the approach I was thinking of. I think if we add localization, we'll need to rethink how abbreviated months are done, since it currently just takes the first the first three letters of the month, which isn't in line with how other languages handle the abbreviation. My guess is that the only fully localized way to handle that will be with a separate array of abbreviations. Apart from that consideration, the only other thing I can think of is adding a few more tests to make sure that the language specific set/get are working (tests for calling |
I don't think how months are being abbreviated is a problem. I'm pretty sure the first three letters convention is world-wide accepted. I'd be worried about pluralization instead. In the future, for newer formats, we may need some "internationalized" helper for pluralization. Since, for instance, the singular for
In regards to that, we must discuss if |
@Frondor Looks Nice. I just wondering if it's better returning a new function instead of a new instance.
|
I agree that it makes sense to keep that portion in English. Unless I'm reading the PR incorrectly (I may be, correct me if I am), the changes at index.js:137-156 will change the Would a pluralizer function that can be overridden in the localization work (passed as a piece in the locale config)? So for English the default function would return the input string + 's', but for other languages you could handle the edge cases directly in the function with if tests. The trade off would be that the library would stay super small since the English function could just be one line, but the pluralizer function might be unwieldy in other languages. |
if
I think you may be looking at the first commit. I committed a mistake, but latter switched back to
Pretty much, it can even be shipped within |
Ah, you're right, I was looking at the wrong commit, my bad. Looks good to me! |
@Frondor Another idea here. We might better give our user a choice whether to set local in current line of code, a new instance, or set global. A pseudo code here: let local = 'Monday'
class Dayjs {
constructor(config, local) {
this.local = null
if (local) this.local = local
}
setGlobalLocal(l) {
local = l
}
format() {
return this.local || local
}
}
const dayjs = (config, local) => (new Dayjs(config, local))
dayjs.newLocalDayjs = (local) => (config) => (new Dayjs(config, local))
export default dayjs And usage: import { spanishDay, FrenchDay } from 'dayjs/locales'
// 1
dayjs() // normal dayjs
// 2
dayjs(20180808, spanishDay) // set local in this line only
dayjs() // still normal dayjs
// 3
dayjs.setGlobalLocal(spanishDay) // dayjs turns to spanish
dayjs() // spanish dayjs
// 4
const FrenchDayjs = dayjs.newLocalDayjs(FrenchDay) // get a new FrenchDayjs function
FrenchDayjs() // french dayjs
dayjs() // still normal dayjs @schiem @SBoudrias opinions? |
I don't think we should encourage users to set different locales per instance. Remember every time you provide locales you're doing At this moment (in my PR), the whole cycle lets you add / switch / override locales three times: I know it's out of the scope of this issue (probably should be addressed by another PR), but I'll show an "advanced" use case for this lib; we've building a lightweight ORM and my goal is to provide date casting with You may ask why not We can easily solve that by doing something like this:
import { Dayjs }, dayjs from 'dayjs'
const FrenchDayjs= Dayjs({ format: 'YYYY', locale: frFR, date: nullValue || new Date() })
// or
const date = dayjs(new Date())
console.log(JSON.stringify(Dayjs)) // prints "2018" because it internally uses obj.toJSON() which uses default format provided instead of current toISOString() You can see how this kind of use case benefits from the new config here (note the class Car extends Model {
get dates () {
return {
created_at: 'dddd of YYYY',
updated_at: ''
}
}
}
const car = new Car({
created_at: new Date(2018, 3, 28),
updated_at: new Date(2018, 3, 29)
})
const year = car.created_at.format('YYYY') // "2018"
const serialize = JSON.stringify(car) // '{created_at: "Saturday of 2018"}' TL;DRWhat I suggest is a set of (backwards compatible) changes in favor of performance and flexibility
If some of you is unsure about what I mean (my english sucks, I know), I can create another branch to display the changes, just ask for that 😉 |
We might could just use direct assignment instead of Object.assign. Besides, Object.assign is an
As far as I know, a nodejs server shares the same global environment for every incoming request. According to @SBoudrias' note, changing the global |
Still, I'm afraid I didn't get what you mean. Sorry. May be I have to go through your code and comment again tomorrow. Questions,
Plus, any direct opinion or suggestion to my pseudo code above? Looking for a feed back. :D Good job so far. I think we are close to the FINAL locales we are looking for. |
I knew the ORM example would only bring more confusion to my horrible English 😅 I'll try to be more compact this time.
Because userCollection.map(user => {
user.created_at.format()
return user
} I'm afraid this is a caveat in moment.js as well, but to be honest, I find unnecessarily redundant to have a
Added some text in the HTML panel of that codepen for you, so I don't bloat this comment (pay a look to
I agree! We probably need to move the default language constants from
I'm not sure if this solves the issue you're referring to, but personally, in node, I'd always try to avoid loading locales per-request. I'd rather create a |
Looks similar to my approach, but the export class Dayjs { constructor (config) {...} }
export default const dayjs = (date, config) => new Dayjs(config) } // config.date = date and let the user handle the instance however he wants (see my |
My mistake. It's an imported object not a string. I've corrected the code above.
Export Plus, I think there's no doubt that we should add locales and plugins to dayjs at the moment. And I want to discuss the API design. We might keep the tradition style : dayjs.local(French)
dayjs.extend(AdvancedFormat) or just one api for less confusion. In this way, a locale is also a kind of plugin: import { LocaleFrench, AdvancedFormat } from 'dayjs/plugin'
dayjs.use(LocaleFrench)
dayjs.use(AdvancedFormat) How do you think. |
I agree with both ideas. At the moment, we don't have You can also do
Now that I think, I prefer your first example with |
OK, here comes a problem. How can we set a global locale config? Any suggestion about a global configuration? dayjs.local(esES)
dayjs.format() // esES by default
dayjs.format() // esES |
Plus, @Frondor @schiem would you mind making a new pull request to branch We are currently implementing two important features locales as well as plugin system. And our code might dependence others. I want get everything(test, docs, rollup config...) done before merge to our main branch. Thanks |
@iamkun Absolutely. Would you prefer a new PR or just modifying the existing one? |
Alright, we can't change the target branch, so I have to close this PR and send a new one to the new branch, it actually targets Anyway, what's the plugin system? Can't we tackle it by just creating an
Actually, I think there's not way we can do that without using globals or env variables (I recommend none of both). I suggest to encourage the user to create base instances with its own config, and then I've written a full example of what's done so far in my PR, #77 and new approachs to some methods |
@Frondor The original intent behind the plugins was to also allow overwriting built in methods, e.g. allowing more format options without needing to add bloat to the core. So you could still call
The way that I handled that for the plugin system was by having an instance set it on the prototype, like:
Could overwriting a property of the prototype be done to set locales? |
@schiem you can take a look to the Vue approach to plugins, you may extract some ideas from there. I personally like how the install method just receives the
Can't see the necessity for that though, the object locales is always living in the |
Locales released in 1.6.0 |
will there be any locales avaliable?
The text was updated successfully, but these errors were encountered: