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
s/lang/locale #1761
s/lang/locale #1761
Conversation
lang : deprecate( | ||
"moment().lang is deprecated. Use moment().locale instead.", | ||
function (key) { | ||
return this.locale(key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this should actually return localeData()
to be keep the old API, so I need to fix that. The new one, where moment().locale()
just returns the name, but moment().localeData()
returns the Locale instances, is more consistent with the behavior of the library-level functions, so this is just a matter of changing what the deprecation does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense.
Looks like I'll need to rebase. I'll do that after I get some feedback. |
@@ -0,0 +1,18 @@ | |||
0 info it worked if it ends with ok |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this file :)
Looks great. You can merge after the rebase. |
* Deprecated moment.lang(), moment.langData(), moment#lang, and duration#lang * Added moment.locale(), moment.localeData(), moment#localeData, and druation#localeData * Added moment.defineLocale() that defines a locale but doesn't set it as the global locale (moment#1750) * Removed the hack in the language concatenator that set the language to English. * Refactored internal code to use moment#localeData instead of local functions, and set the language directly on the moment instance at creation. * Moved all the files and changed the build scripts so that everything lives is named "locale" instead of "lang", e.g. the locale files under the "locale" directory. * I did *not* include build-generated changes like component.json and the concated locale files, but I did inspect them to see that they look right.
@ichernev Fixed except for the one additional comment I made. |
OK, so lets figure out the case with the default language: If you don't include any language filesUse English. Not that you can use anything else If you have included only one language fileOption 1: Use that language If you have included more than one fileOption 1: Use last included language (consistent with the case with one language) To make everybody happy we can have a flag What do you think? |
I definitely think that if we pick option 2, we have to pick it in both cases. Otherwise it's too complicated to explain. I'm not sure I followed that last part:
I guess that means this? var backToOldLocale = require('locale/fr', {just_load: true}); //or however you pass options to require
moment.locale(); //=> 'fr'
backToOldLocale();
moment.locale(); //=> 'en' If so, I'm not a huge fan:
I don't think there's anything really bad about making the user call |
…eturn statement in the locale definitions.
@ichernev Removed those tests. |
@ichernev another issue. From your comment in #1750: moment.lang('fr'); // sets the default for all newly created moments
// unless a language has been explicitly specified
// in the constructor That is also intuitive to me and how I made this PR behave without even thinking about it. But I just realized from the discussion and tests in #1785, that that's actually not how moment is designed to behave; > var moment = require('./moment');
> var inEnglish = moment()
> moment.lang('fr')
'fr'
> inEnglish.lang()._abbr
'fr' We don't have great tests for that, but it now occurs to me that the code (before this PR), goes out of its way to ensure that, and our cloning logic even takes it into account. It sort of makes sense; we're setting the language on the prototype, and it's thus being inherited everywhere. And if you're programmatically switching languages in an entire UI, you could conceivably want to set all of the locales on all of the moment objects you have sitting around as UI state. But in another sense it's totally wack, because all this prototype stuff is hidden away as implementation details, and they don't have a way to tell between In this PR: > var moment = require('./moment');
> var inEnglish = moment()
> inEnglish.locale()
'en'
> moment.locale('fr')
'fr'
> inEnglish.locale()
'en' To me, that's on balance an improvement. But it is a big breaking change. What do you think? /cc @timrwood, who probably has a better perspective on this. |
Originally, the locale was just a global option you could set. It wasn't until I don't know if there is much of a use case for creating a moment without a locale and then expecting it to stay up to date with the global locale when the global locale changes. Looking back on it, it does feel a bit unexpected. I think an additional deprecation note in the 'upgrade to 3.0' changelog would suffice. |
I agree. As as an aside, if we make that change, we should move the global setting from |
just_loadOK, first about the I've seen other libraries do it more explicitly -- you take the main library, you take the language module, and then you tell the library to use the module. In this case you have full control over how exactly to load it (should it also switch or not). So now the options are:
lang existing behaviorTo be honest, I never really understood how that worked until now. I've tried to follow the code a few times and every single time I gave up as it didn't make much sense (and I didn't have enough time). So I think changing it is something we can do, and it shouldn't break a lot of workflows. Also about the change being implemented with prototype -- it should be easy to enable passing The problem is this PR is already pretty big, and if we have to change it back to prototype-hack-global-lang and then again after 3.0 -- makes no sense. Now we use |
Yeah, definitely use |
I'd prefer that the user sets the language explicitly; I don't like that the behavior of my code depends on things like what order I put the JS in, and the whole thing feels flimsy and non-obvious. But I do see that it's a real breaking change, so I can put in some hacks for now. So my proposal on all things:
Sound good? |
That makes sense. I hope its not too different than the current diff :) |
@ichernev Done! |
Looks great. Merging! |
duration#lang
druation#localeData
as the global locale (Instance-level locale leaks to other instances #1750)
to English.
functions, and set the language directly on the moment instance at
creation.
lives is named "locale" instead of "lang", e.g. the locale files under
the "locale" directory.
the concated locale files, but I did inspect them to see that they look
right. But I did remove the old ones wherever they won't be named correctly in the future.