Moment: λ‘œμΌ€μΌ 변경이 μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€

에 λ§Œλ“  2016λ…„ 02μ›” 15일  Β·  22μ½”λ©˜νŠΈ  Β·  좜처: moment/moment

λ‘œμΌ€μΌ 값을 λ³€κ²½ν•˜λ €κ³  ν•˜λ©΄ 였λ₯˜λ„ λ°œμƒν•˜μ§€ μ•Šκ³  아무 일도 μΌμ–΄λ‚˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

      this.$log.log(moment.locale()); // returns 'en'
      this.$log.log(moment.locale('fr')); // set to french but returns 'en'
      this.$log.log(moment.locale()); // of course, still returns 'en' instead of 'fr'

Chrome 개발자 도ꡬ μ½˜μ†”μ—μ„œλ„ μ‹œλ„ν–ˆμŠ΅λ‹ˆλ‹€.

> moment.locale('fr')
-> "en"
Troubleshooting

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λ‚˜λŠ” μΆ”κ°€ν–ˆλ‹€:
import 'moment/locale/fr';

λ‹€μŒμ„ μ‚¬μš©ν•˜μ—¬:
moment.locale('fr')

이제 λ‘œμΌ€μΌμ„ ν”„λž‘μŠ€μ–΄λ‘œ μ˜¬λ°”λ₯΄κ²Œ μ„€μ •ν•©λ‹ˆλ‹€.
λ™μ μ΄μ§€λŠ” μ•Šμ§€λ§Œ ν˜„μž¬λ‘œμ„œλŠ” μž‘λ™ν•  κ²ƒμž…λ‹ˆλ‹€.

λ‚˜λŠ” 그것을 닫을 수 μžˆλ‹€κ³  μƒκ°ν•˜κ³  λ‹΅λ³€ ν•œ λͺ¨λ“  μ‚¬λžŒλ“€μ—κ²Œ κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€.
이 λ¬Έμ œκ°€ TypeScript/Angular/Jspm을 μ‚¬μš©ν•˜λŠ” μ‚¬λžŒλ“€μ—κ²Œ 도움이 되기λ₯Ό λ°”λžλ‹ˆλ‹€.

λͺ¨λ“  22 λŒ“κΈ€

λ‹€λ₯Έ μˆœκ°„μ„ μ œκ³΅ν•  수 μžˆλŠ” λͺ¨λ“ˆ λ‘œλ”λ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆκΉŒ?

import m from 'moment';

this.$log.log(m.locale());
this.$log.log(m.locale('fr'));
this.$log.log(m.locale());

this.$log.log(window.moment.locale());
this.$log.log(window.moment.locale('is'));
this.$log.log(window.moment.locale());

μ €λŠ” 각도 ν”„λ‘œμ νŠΈμ— 있으며 λͺ¨λ“ˆμ€ JSPM을 μ‚¬μš©ν•˜λŠ” ν•Έλ“€μž…λ‹ˆλ‹€.
λ‚˜λŠ” 당신이 "그것은 λ‹Ήμ‹ μ—κ²Œ λ‹€λ₯Έ μˆœκ°„μ„ 쀄 수 μžˆμŠ΅λ‹ˆλ‹€"λΌλŠ” 말의 의미λ₯Ό μ΄ν•΄ν•˜μ§€ λͺ»ν•©λ‹ˆλ‹€.

3쀄은 λ™μΌν•œ λ°©λ²•μœΌλ‘œ λ™μΌν•œ μˆœκ°„μ„ μ‚¬μš©ν•˜μ—¬ μ‹œλ„ν•œ μ‹€μ œ μ½”λ“œμž…λ‹ˆλ‹€.
νŠΉμ • λ³€μˆ˜λ₯Ό μ‚¬μš©ν•΄λ„ 같은 κ²°κ³Όκ°€ λ‚˜μ˜΅λ‹ˆλ‹€.

import * as moment from 'moment';

export function registerDateFormatService(appModule: angular.IModule) {
   appModule.service('dateFormatService', DateFormatService);
}

export class DateFormatService {
   constructor(private $log: ng.ILogService) {
   }

   public format(value: moment.Moment, displayFormat: string): string {
      this.$log.log(value.locale());
      this.$log.log(value.locale('fr'));
      this.$log.log(value.locale());
      return value.format(displayFormat);
   }
}

κ²°κ³ΌλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

angular.js:13239 Moment {_isAMomentObject: true, _i: "2010-10-20 4:30", _f: "YYYY-MM-DD", _isUTC: false, _pf: Object…}_d: Wed Oct 20 2010 00:00:00 GMT+0200 (CEST)_f: "YYYY-MM-DD"_i: "2010-10-20 4:30"_isAMomentObject: true_isUTC: false_isValid: true_locale: Locale_abbr: "en"_ordinalParse: /\d{1,2}(th|st|nd|rd)/_ordinalParseLenient: /\d{1,2}(th|st|nd|rd)|\d{1,2}/ordinal: (number)__proto__: Locale_pf: Object__proto__: Moment
angular.js:13239 en

λ‘œμΌ€μΌ 데이터λ₯Ό κ°€μ Έμ˜€κ³  μžˆμŠ΅λ‹ˆκΉŒ? μ–΄λ–€ .js νŒŒμΌμ„ μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆκΉŒ?

λ‹€μŒ 쀄 뒀에 λ‹€λ₯Έ κ°€μ Έμ˜€κΈ°λ₯Ό μΆ”κ°€ν•˜λŠ” 것을 μ˜λ―Έν•©λ‹ˆκΉŒ?
* 'μˆœκ°„'μ—μ„œ μˆœκ°„μœΌλ‘œ κ°€μ Έμ˜€κΈ°;

λ˜λŠ” μ—¬κΈ°μ—μ„œ JSPM을 μ‚¬μš©ν•˜μ—¬ λͺ¨λ“ˆ κ°μ§€μ—μ„œ κ°€μ Έμ˜€κΈ°.
(λ‚΄ κ²½μš°μ—λŠ” jspm λͺ¨λ“ˆ 폴더에 moment.js와 fr.js와 같은 localexx.js처럼 λ³΄μ΄λŠ” 것이 많이 μžˆμŠ΅λ‹ˆλ‹€.)

λ¬Έμ„œμ˜ "μ‚¬μš© μœ„μΉ˜" μ„Ήμ…˜μ„ 읽고 μ΄λŸ¬ν•œ μ‹œλ‚˜λ¦¬μ˜€κ°€ κ·€ν•˜μ—κ²Œ μ μš©λ˜λŠ”μ§€ ν™•μΈν•˜μ‹­μ‹œμ˜€. 일반적으둜 moment-with-locales.js λ₯Ό μ‚¬μš©ν•˜κ±°λ‚˜ λ‘œμΌ€μΌ 데이터λ₯Ό λ³„λ„λ‘œ λ‘œλ“œν•΄μ•Ό ν•©λ‹ˆλ‹€. λŒ€λΆ€λΆ„μ˜ λ‘œλ”λŠ” μ–Έμ–΄ νŒŒμΌμ„ μžλ™μœΌλ‘œ μ„ νƒν•˜μ§€ μ•ŠμœΌλ―€λ‘œ λ³„λ„λ‘œ 가져와야 ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

기본적으둜 λ‘œλ“œλ˜μ§€ μ•Šμ€ λ‘œμΌ€μΌλ‘œ μ „ν™˜ν•˜λ €κ³  ν•˜λ©΄ μˆœκ°„μ΄ en λ‘œμΌ€μΌλ‘œ λ˜λŒμ•„κ°‘λ‹ˆλ‹€.

"λΈŒλΌμš°μ €μ—μ„œ λ‘œμΌ€μΌ λ‘œλ“œ" 도 μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€.

λ‚΄κ°€ ν•  수 μžˆλŠ” 일을 μ‹€ν—˜ν•΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.
λ‚˜λŠ” npm install을 톡해 jspmμ—μ„œ μ‚¬μš©λ˜λŠ” package.json에 μΆ”κ°€λœ κ°„λ‹¨ν•œ 라인을 μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
"moment": "github:moment/moment@^2.11.2"

그런 λ‹€μŒ 단일 κ°€μ Έμ˜€κΈ°μ™€ ν•¨κ»˜ μ‚¬μš©ν•©λ‹ˆλ‹€.
import * as moment from 'moment';

λ‚˜λŠ” λ™μΌν•œ μ‚¬μš©λ²•μ„ κ°€μ§ˆ 수 μžˆλŠ” 미래의 μ‚¬μš©μžλ₯Ό μœ„ν•΄ μΆ”μ ν•˜κΈ° μœ„ν•΄ 이 λŒ“κΈ€μ— μΌμŠ΅λ‹ˆλ‹€.
μ‹€ν—˜ κ²°κ³Όλ₯Ό ν¬μŠ€νŒ…ν•˜κ² μŠ΅λ‹ˆλ‹€.
감사 ν•΄μš”

λ‚˜λŠ” μΆ”κ°€ν–ˆλ‹€:
import 'moment/locale/fr';

λ‹€μŒμ„ μ‚¬μš©ν•˜μ—¬:
moment.locale('fr')

이제 λ‘œμΌ€μΌμ„ ν”„λž‘μŠ€μ–΄λ‘œ μ˜¬λ°”λ₯΄κ²Œ μ„€μ •ν•©λ‹ˆλ‹€.
λ™μ μ΄μ§€λŠ” μ•Šμ§€λ§Œ ν˜„μž¬λ‘œμ„œλŠ” μž‘λ™ν•  κ²ƒμž…λ‹ˆλ‹€.

λ‚˜λŠ” 그것을 닫을 수 μžˆλ‹€κ³  μƒκ°ν•˜κ³  λ‹΅λ³€ ν•œ λͺ¨λ“  μ‚¬λžŒλ“€μ—κ²Œ κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€.
이 λ¬Έμ œκ°€ TypeScript/Angular/Jspm을 μ‚¬μš©ν•˜λŠ” μ‚¬λžŒλ“€μ—κ²Œ 도움이 되기λ₯Ό λ°”λžλ‹ˆλ‹€.

μ•„λ§ˆλ„ 이것을 λ¬Έμ„œμ— μΆ”κ°€ν•˜μ‹­μ‹œμ˜€. λ‚˜λŠ” 같은 λ¬Έμ œκ°€ μžˆμ—ˆκ³  점점 더 λ§Žμ€ μ‚¬λžŒλ“€μ΄ Bower λŒ€μ‹  jspm을 μ‚¬μš©ν•˜κΈ° μ‹œμž‘ν–ˆμŠ΅λ‹ˆλ‹€.

μž‘λ™ν•˜κ²Œ λ˜μ–΄ κΈ°μ©λ‹ˆλ‹€! λ¬Έμ„œ μ €μž₯μ†Œμ— 문제λ₯Ό μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€. 거기에 자유둭게 PR을 λ³΄λ‚΄μ‹­μ‹œμ˜€.

κ·Έλ ‡κ²Œ μˆ˜μž…
'μˆœκ°„/src/λ‘œμΌ€μΌ/fr-ca' κ°€μ Έμ˜€κΈ°;
import 'μˆœκ°„/src/locale/en-ca';

@younss μ—κ²Œ 감사 효과적 μ΄μ—ˆμŠ΅λ‹ˆλ‹€. Ionic 2 RC 2λ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

@younss 당신은 λ‚˜μ˜ κ΅¬μ„Έμ£Όμž…λ‹ˆλ‹€! 2017λ…„ 10μ›” 10일이고 λ¬Έμ„œμ—μ„œλŠ” 이 λ³€κ²½ 사항을 μ§€μ •ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€! μˆœκ°„ 2.18μ—μ„œ 2.19.0으둜 μ—…λ°μ΄νŠΈλ˜κ³  λ‘œμΌ€μΌ κ°€μ Έμ˜€κΈ°κ°€ μ™„μ „νžˆ μ€‘λ‹¨λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
λ¦¬μ•‘νŠΈ μ‚¬μš©ν•˜κΈ°.
λ‹€μ‹œ ν•œ 번 κ°μ‚¬ν•©λ‹ˆλ‹€!

2.19.1둜 μ—…λ°μ΄νŠΈν–ˆμ„ λ•Œλ„ 같은 λ¬Έμ œκ°€ μžˆμ—ˆμŠ΅λ‹ˆλ‹€.
μ–΄μ©Œλ©΄ 이것이 도움이 될 수 μžˆμŠ΅λ‹ˆλ‹€:

μ œκ±°ν•˜λ‹€ :

  • node_modules 폴더
  • νŒ¨ν‚€μ§€ 잠금.json

μž¬μ‹€ν–‰:
npm μ„€μΉ˜

λ‚˜λŠ” import 'μˆœκ°„'을 μ •μƒμ μœΌλ‘œ μœ μ§€ν•˜κ³  λ‚΄ μ†ŒμŠ€μ—μ„œ λ‘œμΌ€μΌμ„ μ‚¬μš©ν•©λ‹ˆλ‹€. λ”°λΌμ„œ @younss 처럼 κ°€μ Έμ˜¬ ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€.
VueJSλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.
이것은 λ‚˜λ₯Ό μœ„ν•΄ μΌν–ˆμŠ΅λ‹ˆλ‹€.

pbκ°€ μˆœκ°„ λ…Έλ“œ 데이터λ₯Ό μ œλŒ€λ‘œ μ—…λ°μ΄νŠΈν•˜μ§€ μ•ŠλŠ” package-lock.json νŒŒμΌμ— μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€. 이λ₯Ό μ œκ±°ν•˜κ³  npm install을 λ‹€μ‹œ μ‹€ν–‰ν•˜λ©΄ λͺ¨λ“  데이터가 μ—…λ°μ΄νŠΈλœ μƒˆ package-lock.json 파일이 μƒμ„±λ©λ‹ˆλ‹€.

@younss , κ°μ‚¬ν•©λ‹ˆλ‹€, μ €μ—κ²Œ νš¨κ³Όμ μž…λ‹ˆλ‹€. λ‚˜λŠ” μ†Œν¬ λ²ˆλ“€λŸ¬λ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

이 μ†”λ£¨μ…˜μ€ μ €μ—κ²Œ νš¨κ³Όμ μ΄μ—ˆμŠ΅λ‹ˆλ‹€. μ—¬κΈ°

λ‚˜λŠ” μΆ”κ°€ν–ˆλ‹€:
'μˆœκ°„/λ‘œμΌ€μΌ/fr' κ°€μ Έμ˜€κΈ°;

이게 또 λ°”λ€Œμ—ˆλ‚˜μš”?
λ‚˜λŠ” μ„€μΉ˜ν–ˆλ‹€:

npm i moment-with-locales-es6
npm i moment

ν•˜λ‚˜μ˜ ν•¨μˆ˜λ‘œ 클래슀λ₯Ό λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.

import * as moment from 'moment';
import 'moment/locale/pt-br';
import 'moment/locale/fr-ca';

export class DatetimeFormatPipe
    transform(date: string): string {
        moment.locale('pt-BR');
        console.log(moment.locale()); 
        console.log('pt-BR', moment(date).toLocaleString());

        moment.locale('fr-CA');
        console.log(moment.locale());
        console.log('fr-CA', moment(date).toLocaleString());

        return "";
    }
}

λ‘œκ·ΈλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

pt-br
pt-BR 2016λ…„ 12μ›” 20일 ν™”μš”μΌ 10:45:12 GMT+0100
fr-ca
fr-CA ν™” 2016λ…„ 12μ›” 20일 10:45:12 GMT+0100

κ·Έλ ‡κ²Œ μˆ˜μž…
'μˆœκ°„/src/λ‘œμΌ€μΌ/fr-ca' κ°€μ Έμ˜€κΈ°;
import 'μˆœκ°„/src/locale/en-ca';

그것은 λ‚˜λ₯Ό μœ„ν•΄ μž‘λ™ν•©λ‹ˆλ‹€ κ°μ‚¬ν•©λ‹ˆλ‹€!

λ‚˜λŠ” μ‚¬μš©ν•œλ‹€:

import moment from 'moment/dist/moment';
import fr from 'moment/dist/locale/fr';

moment.locale('fr', fr);

μž‘λ™ν•©λ‹ˆλ‹€.
λ‹€λ₯Έ μ†”λ£¨μ…˜μ€ μ €μ—κ²Œ νš¨κ³Όκ°€ μ—†μ—ˆμŠ΅λ‹ˆλ‹€.

import moment from 'moment/dist/moment';
import 'moment/dist/locale/ru';

moment.locale('ru');

였직 λ‚˜λ₯Ό μœ„ν•΄ μž‘λ™ν•©λ‹ˆλ‹€. @ligne13 힌트 κ°μ‚¬ν•©λ‹ˆλ‹€!

μˆœκ°„ 2.25.2

μ•ˆλ…•ν•˜μ„Έμš”,

'import 'moment/locale/fr';' μ‚¬μš© μ‹œ λ‚΄ Node/Express μ•±μ—μ„œ "SyntaxError: Cannot use import statement outside the module" 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” 이것을 λ΄€κ³  --experimental-modules --es-module-specifier-resolution=node ν”Œλž˜κ·Έλ₯Ό μΆ”κ°€ν•˜λ €κ³  μ‹œλ„ν–ˆμ§€λ§Œ 이제 λ‚΄ 앱이 μ—‰λ§μž…λ‹ˆλ‹€.

이 κ°€μ Έμ˜€κΈ°λ₯Ό μΆ”κ°€ν•  수 μ—†λŠ” 경우 λ‘œμΌ€μΌμ„ λ³€κ²½ν•  수 μžˆλŠ” λ‹€λ₯Έ 방법이 μžˆμŠ΅λ‹ˆκΉŒ?

감사 ν•΄μš”

폴


사양:

  • OS : WSL2λ₯Ό ν†΅ν•œ μš°λΆ„νˆ¬ 20.04
  • λ…Έλ“œ : 12.18.3
  • NPM : 6.14.6
  • μ΅μŠ€ν”„λ ˆμŠ€ : 4.16.1
  • λͺ¨λ©˜νŠΈJS : 2.27

μ•ˆλ…•ν•˜μ„Έμš”,

'import 'moment/locale/fr';' μ‚¬μš© μ‹œ λ‚΄ Node/Express μ•±μ—μ„œ "SyntaxError: Cannot use import statement outside the module" 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” 이것을 λ΄€κ³  --experimental-modules --es-module-specifier-resolution=node ν”Œλž˜κ·Έλ₯Ό μΆ”κ°€ν•˜λ €κ³  μ‹œλ„ν–ˆμ§€λ§Œ 이제 λ‚΄ 앱이 μ—‰λ§μž…λ‹ˆλ‹€.

이 κ°€μ Έμ˜€κΈ°λ₯Ό μΆ”κ°€ν•  수 μ—†λŠ” 경우 λ‘œμΌ€μΌμ„ λ³€κ²½ν•  수 μžˆλŠ” λ‹€λ₯Έ 방법이 μžˆμŠ΅λ‹ˆκΉŒ?

감사 ν•΄μš”

폴

사양:

  • OS : WSL2λ₯Ό ν†΅ν•œ μš°λΆ„νˆ¬ 20.04
  • λ…Έλ“œ : 12.18.3
  • NPM : 6.14.6
  • μ΅μŠ€ν”„λ ˆμŠ€ : 4.16.1
  • λͺ¨λ©˜νŠΈJS : 2.27

@pbrissaud

아직 node.jsμ—μ„œλŠ” 'κ°€μ Έμ˜€κΈ°'λ₯Ό μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€. λ‚˜λŠ” 그것을 직접 μ‹œλ„ν•˜μ§€ μ•Šμ•˜μ§€λ§Œ 이것이 νš¨κ³Όκ°€ μžˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

require('moment/locale/fr')
moment.locale('fr');
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰