Skip to content

Commit

Permalink
Revert "chore: Converts internals of ember-intl to es6 classes (#600)"
Browse files Browse the repository at this point in the history
This reverts commit 16ec0ba.
  • Loading branch information
jasonmit committed Aug 30, 2018
1 parent 0954956 commit 8900971
Show file tree
Hide file tree
Showing 27 changed files with 572 additions and 848 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
@@ -1,6 +1,9 @@
module.exports = {
root: true,
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2017,
sourceType: 'module'
},
plugins: ['prettier', 'ember'],
extends: ['eslint:recommended', 'plugin:ember/recommended'],
env: {
Expand Down
7 changes: 5 additions & 2 deletions .travis.yml
Expand Up @@ -18,8 +18,10 @@ env:
- JOBS=1
matrix:
# we recommend testing LTS's and latest stable release (bonus points to beta/canary)
- EMBER_TRY_SCENARIO=ember-lts-2.4
- EMBER_TRY_SCENARIO=ember-lts-2.8
- EMBER_TRY_SCENARIO=ember-lts-2.18
- EMBER_TRY_SCENARIO=ember-lts-2.12
- EMBER_TRY_SCENARIO=ember-lts-2.16
- EMBER_TRY_SCENARIO=ember-release
- EMBER_TRY_SCENARIO=ember-beta
- EMBER_TRY_SCENARIO=ember-canary
Expand All @@ -28,6 +30,7 @@ env:
matrix:
fast_finish: true
allow_failures:
- env: EMBER_TRY_SCENARIO=ember-lts-2.4
- env: EMBER_TRY_SCENARIO=ember-lts-2.8
- env: EMBER_TRY_SCENARIO=ember-canary

Expand All @@ -41,7 +44,7 @@ install:
script:
- yarn lint:js
- yarn tests-node
- node_modules/.bin/ember try $EMBER_TRY_SCENARIO --skip-cleanup
- node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO --skip-cleanup

before_deploy:
- yarn build -- --environment=production
Expand Down
121 changes: 48 additions & 73 deletions README.md
Expand Up @@ -49,16 +49,14 @@ homepage:
This is can be done at any point within your app boots. This is typically done within your Application route's `beforeModel` hook by calling `intl.setLocale('en-us')` [Read more about the Service API](https://github.com/ember-intl/ember-intl/blob/2.x/docs/ember-service-api.md).

```js
// app/routes/application.js
class ApplicationRoute extends Route {
@service
intl;

beforeModel() {
/* NOTE: if you lazily load translations, here is also where you would load them via `intl.addTranslations` */
return this.get('intl').setLocale(['fr-fr', 'en-us']); /* array optional */
}
}
// app/routes/application.js
export default Route.extend({
intl: service(),
beforeModel() {
/* NOTE: if you lazily load translations, here is also where you would load them via `intl.addTranslations` */
return this.get('intl').setLocale(['fr-fr', 'en-us']); /* array optional */
}
});
```

## Helper Examples
Expand All @@ -82,17 +80,15 @@ photos:
**Service API**

```js
class PhotoComponent extends Component {
@service
intl;
export default Component.extend({
intl: service(),

@computed('intl.locale', 'model.photos.[]')
get banner() {
banner: computed('intl.locale', 'model.photos.length', function() {
return this.get('intl').t('photos.banner', {
photos: this.get('model.photos.length')
});
}
}
})
});
```

### Format Number
Expand All @@ -107,15 +103,12 @@ Formats numbers using [`Intl.NumberFormat`][Intl-NF], and returns the formatted
Or programmatically convert a number within any Ember Object.

```js
class CostComponent extends Component {
@service
intl;

@computed('intl.locale', 'cost')
get computedNumber() {
return this.get('intl').formatNumber(this.get('cost') /*, optional options hash */);
}
}
export default Component.extend({
intl: service(),
computedNumber: computed('intl.locale', 'cost', function() {
return this.get('intl').formatNumber(this.get('cost')/*, optional options hash */);
})
});
```

#### Format Number Options
Expand All @@ -134,15 +127,12 @@ Formats dates using [`Intl.DateTimeFormat`][Intl-DTF], and returns the formatted
Or programmatically convert a date within any Ember Object.

```js
class DateComponent extends Component {
@service
intl;

@computed('intl.locale')
get currentDate() {
export default Component.extend({
intl: service(),
computedNow: computed('intl.locale', function() {
return this.get('intl').formatDate(new Date()/*, optional options hash */);
}
}
})
});
```

#### Format Date Options
Expand All @@ -162,15 +152,12 @@ Or programmatically convert a time within any Ember Object.

```js
// example
class TimeComponent extends Component {
@service
intl;

@computed('intl.locale')
get computedNow() {
export default Component.extend({
intl: service(),
computedNow: computed('intl.locale', function() {
return this.get('intl').formatTime(new Date()/*, optional options hash */);
}
}
})
});
```

#### Format Time Options
Expand All @@ -181,14 +168,13 @@ class TimeComponent extends Component {
Formats dates relative to "now" using [`IntlRelativeFormat`][Intl-RF], and returns the formatted string value.

```js
class RelativeExampleContainer extends Component {
get timestamp() {
export default Component.extend({
timestamp: computed(function() {
let date = new Date();
date.setDate(date.getDate() - 3);

return date;
})
}
});
```

```hbs
Expand All @@ -198,21 +184,13 @@ class RelativeExampleContainer extends Component {
Or programmatically convert a relative time within any Ember Object.

```js
class RelativeExampleContainer extends Component {
@service
intl;

@computed('intl.locale')
get yesterdayTimestamp() {
export default Component.extend({
intl: service(),
yesterday: computed('intl.locale', function() {
let date = new Date();

return this.get('intl')
.formatRelative(
date.setDate(date.getDate() - 1)
/*, optional options hash */
);
}
}
return this.get('intl').formatRelative(date.setDate(date.getDate() - 1)/*, optional options hash */);
})
});
```

#### Live Relative Timestamp
Expand Down Expand Up @@ -242,25 +220,22 @@ Recompute the relative timestamp on an interval by passing an `interval` argumen
**Service API**

```js
class PhotoExampleContainer extends Component {
@service
intl;

count = 10;

@computed('intl.locale', 'count')
get label() {
export default Component.extend({
intl: service(),
count: 0,
label: computed('intl.locale', 'model.photos.length', function() {
return this.get('intl').formatMessage(`
You took {photos, plural,
You took {numPhotos, plural,
=0 {no photos}
=1 {one photo}
other {# photos}
}
`, {
photos: this.count
`,
{
numPhotos: this.get('model.photos.length')
});
}
}
}).readOnly()
});
```

### Format HTML Message
Expand Down
4 changes: 1 addition & 3 deletions addon/-private/formatters/-base.js
Expand Up @@ -11,7 +11,7 @@ import links from '../../utils/links';

const EMPTY_OBJECT = {};

class FormatterBase {
export default class FormatterBase {
get options() {
return emberArray();
}
Expand Down Expand Up @@ -65,5 +65,3 @@ class FormatterBase {
return this.formatter(locale, formatterOptions).format(value, formatOptions);
}
}

export default FormatterBase;
2 changes: 1 addition & 1 deletion addon/-private/formatters/format-date.js
Expand Up @@ -9,7 +9,7 @@ import Formatter from './-base';

export default class FormatDate extends Formatter {
constructor() {
super(...arguments);
super();
this.formatter = createFormatCache(Intl.DateTimeFormat);
}

Expand Down
2 changes: 1 addition & 1 deletion addon/-private/formatters/format-message.js
Expand Up @@ -13,7 +13,7 @@ const { Handlebars } = Ember;

export default class FormatMessage extends Formatter {
constructor() {
super(...arguments);
super();
this.formatter = createFormatCache(IntlMessageFormat);
}

Expand Down
2 changes: 1 addition & 1 deletion addon/-private/formatters/format-number.js
Expand Up @@ -27,7 +27,7 @@ export default class FormatNumber extends Formatter {
}

constructor() {
super(...arguments);
super();
this.formatter = createFormatCache(Intl.NumberFormat);
}

Expand Down
2 changes: 1 addition & 1 deletion addon/-private/formatters/format-relative.js
Expand Up @@ -11,7 +11,7 @@ import Formatter from './-base';

export default class FormatRelative extends Formatter {
constructor() {
super(...arguments);
super();
this.formatter = createFormatCache(IntlRelativeFormat);
}

Expand Down
1 change: 0 additions & 1 deletion addon/-private/is-array-equal.js
Expand Up @@ -4,7 +4,6 @@ export default function(a, b) {
if (!isArray(a) || !isArray(b)) {
return false;
}

if (a === b) {
return true;
}
Expand Down
34 changes: 19 additions & 15 deletions addon/helpers/-format-base.js
Expand Up @@ -3,24 +3,28 @@
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/

import { isEmpty } from '@ember/utils';
import Helper from '@ember/component/helper';
import { get, getWithDefault } from '@ember/object';
import { service } from '@ember-decorators/service';
import { getOwner } from '@ember/application';
import { isEmpty } from '@ember/utils';
import { getWithDefault } from '@ember/object';

class BaseHelper extends Helper {
@service
intl;
const AbstractHelper = Helper.extend({
intl: null,

constructor() {
super(...arguments);
init() {
if (this.constructor === AbstractHelper) {
throw new Error('FormatHelper is an abstract class, can not be instantiated directly.');
}

get(this, 'intl').on('localeChanged', this, this.recompute);
}
this._super();

this.intl = getOwner(this).lookup('service:intl');
this.intl.on('localeChanged', this, this.recompute);
},

format() {
throw new Error('not implemented');
}
},

compute([value], options) {
if (isEmpty(value)) {
Expand All @@ -34,13 +38,13 @@ class BaseHelper extends Helper {
}

return this.format(value, options);
}
},

willDestroy() {
this._super();

get(this, 'intl').off('localeChanged', this, this.recompute);
this.intl.off('localeChanged', this, this.recompute);
}
}
});

export default BaseHelper;
export default AbstractHelper;
11 changes: 4 additions & 7 deletions addon/helpers/format-date.js
Expand Up @@ -3,15 +3,12 @@
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/

import { get } from '@ember/object';
import BaseHelper from './-format-base';

class DateHelper extends BaseHelper {
allowEmpty = true;
export default BaseHelper.extend({
allowEmpty: true,

format(value, options) {
return get(this, 'intl').formatDate(value, options);
return this.intl.formatDate(value, options);
}
}

export default DateHelper;
});
9 changes: 3 additions & 6 deletions addon/helpers/format-message.js
Expand Up @@ -3,13 +3,10 @@
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/

import { get } from '@ember/object';
import BaseHelper from './-format-base';

class MessageHelper extends BaseHelper {
export default BaseHelper.extend({
format(value, options) {
return get(this, 'intl').formatMessage(value, options);
return this.intl.formatMessage(value, options);
}
}

export default MessageHelper;
});

0 comments on commit 8900971

Please sign in to comment.