Skip to content

Commit

Permalink
deprecate Ember.String.loc and {{loc}}
Browse files Browse the repository at this point in the history
  • Loading branch information
locks committed Nov 11, 2020
1 parent d117723 commit c7526f1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 39 deletions.
1 change: 1 addition & 0 deletions packages/@ember/-internals/glimmer/lib/helpers/loc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { helper } from '../helper';
@param {String} str The string to format.
@see {String#loc}
@public
@deprecated
*/
export default helper(function (params) {
return loc.apply(null, params as any /* let the other side handle errors */);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,69 @@ moduleFor(
}

['@test it lets the original value through by default']() {
this.render(`{{loc "Hiya buddy!"}}`);
expectDeprecation(() => this.render(`{{loc "Hiya buddy!"}}`), /loc is deprecated/);
this.assertText('Hiya buddy!', 'the unlocalized string is correct');
runTask(() => this.rerender());
this.assertText('Hiya buddy!', 'the unlocalized string is correct after rerender');
}

['@test it localizes a simple string']() {
this.render(`{{loc "Hello Friend"}}`);
expectDeprecation(() => this.render(`{{loc "Hello Friend"}}`), /loc is deprecated/);
this.assertText('Hallo Freund', 'the localized string is correct');
runTask(() => this.rerender());
this.assertText('Hallo Freund', 'the localized string is correct after rerender');
}

['@test it takes passed formats into an account']() {
this.render(`{{loc "%@, %@" "Hello" "Mr. Pitkin"}}`);
expectDeprecation(() => {
this.render(`{{loc "%@, %@" "Hello" "Mr. Pitkin"}}`);
}, /loc is deprecated/);
this.assertText('Hello, Mr. Pitkin', 'the formatted string is correct');
runTask(() => this.rerender());
this.assertText('Hello, Mr. Pitkin', 'the formatted string is correct after rerender');
}

['@test it updates when bound params change']() {
this.render(`{{loc simple}} - {{loc personal 'Mr. Pitkin'}}`, {
simple: 'Hello Friend',
personal: 'Hello',
});
this.assertText('Hallo Freund - Hallo, Mr. Pitkin', 'the bound value is correct');
expectDeprecation(() => {
this.render(`{{loc simple}} - {{loc personal 'Mr. Pitkin'}}`, {
simple: 'Hello Friend',
personal: 'Hello',
});
this.assertText('Hallo Freund - Hallo, Mr. Pitkin', 'the bound value is correct');
}, /loc is deprecated/);

runTask(() => this.rerender());
this.assertText(
'Hallo Freund - Hallo, Mr. Pitkin',
'the bound value is correct after rerender'
);

runTask(() => set(this.context, 'simple', "G'day mate"));
this.assertText("G'day mate - Hallo, Mr. Pitkin", 'the bound value is correct after update');
expectDeprecation(() => {
runTask(() => set(this.context, 'simple', "G'day mate"));
this.assertText(
"G'day mate - Hallo, Mr. Pitkin",
'the bound value is correct after update'
);
}, /loc is deprecated/);

runTask(() => set(this.context, 'simple', 'Hello Friend'));
this.assertText('Hallo Freund - Hallo, Mr. Pitkin', 'the bound value is correct after reset');
expectDeprecation(() => {
runTask(() => set(this.context, 'simple', 'Hello Friend'));
this.assertText(
'Hallo Freund - Hallo, Mr. Pitkin',
'the bound value is correct after reset'
);
}, /loc is deprecated/);
}

['@test it updates when nested bound params change']() {
this.render(`{{loc greetings.simple}} - {{loc greetings.personal 'Mr. Pitkin'}}`, {
greetings: {
simple: 'Hello Friend',
personal: 'Hello',
},
});
expectDeprecation(() => {
this.render(`{{loc greetings.simple}} - {{loc greetings.personal 'Mr. Pitkin'}}`, {
greetings: {
simple: 'Hello Friend',
personal: 'Hello',
},
});
}, /loc is deprecated/);
this.assertText('Hallo Freund - Hallo, Mr. Pitkin', 'the bound value is correct');

runTask(() => this.rerender());
Expand All @@ -75,22 +91,26 @@ moduleFor(
'the bound value is correct after rerender'
);

runTask(() => set(this.context, 'greetings.simple', "G'day mate"));
this.assertText(
"G'day mate - Hallo, Mr. Pitkin",
'the bound value is correct after interior mutation'
);
expectDeprecation(() => {
runTask(() => set(this.context, 'greetings.simple', "G'day mate"));
this.assertText(
"G'day mate - Hallo, Mr. Pitkin",
'the bound value is correct after interior mutation'
);
}, /loc is deprecated/);

runTask(() =>
set(this.context, 'greetings', {
simple: 'Hello Friend',
personal: 'Hello',
})
);
this.assertText(
'Hallo Freund - Hallo, Mr. Pitkin',
'the bound value is correct after replacement'
);
expectDeprecation(() => {
runTask(() =>
set(this.context, 'greetings', {
simple: 'Hello Friend',
personal: 'Hello',
})
);
this.assertText(
'Hallo Freund - Hallo, Mr. Pitkin',
'the bound value is correct after replacement'
);
}, /loc is deprecated/);
}

['@test it can be overriden']() {
Expand Down
16 changes: 16 additions & 0 deletions packages/@ember/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,23 @@ function _fmt(str: string, formats: any[]) {
@param {Array} formats Optional array of parameters to interpolate into string.
@return {String} formatted string
@public
@deprecated
*/
export function loc(str: string, formats: any[]): string {
deprecate(
'loc is deprecated, please use a dedicated localization solution like ember-intl. More alternatives listed at https://emberobserver.com/categories/internationalization.',
false,
{
id: 'ember-string.loc',
until: '4.0.0',
for: 'ember-source',
url: 'https://deprecations.emberjs.com/v3.x#toc_ember-string-loc',
since: {
available: '3.24',
},
}
);

if (!Array.isArray(formats) || arguments.length > 2) {
formats = Array.prototype.slice.call(arguments, 1);
}
Expand Down Expand Up @@ -324,6 +339,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for @ember/string
@static
@private
@deprecated
*/
loc: {
configurable: true,
Expand Down
16 changes: 10 additions & 6 deletions packages/@ember/string/tests/loc_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
let oldString;

function test(assert, given, args, expected, description) {
assert.equal(loc(given, args), expected, description);
if (ENV.EXTEND_PROTOTYPES.String) {
assert.deepEqual(given.loc(...args), expected, description);
}
expectDeprecation(() => {
assert.equal(loc(given, args), expected, description);
if (ENV.EXTEND_PROTOTYPES.String) {
assert.deepEqual(given.loc(...args), expected, description);
}
}, /loc is deprecated/);
}

moduleFor(
Expand Down Expand Up @@ -69,8 +71,10 @@ moduleFor(
}

['@test works with argument form'](assert) {
assert.equal(loc('_Hello %@', 'John'), 'Bonjour John');
assert.equal(loc('_Hello %@ %@', ['John'], 'Doe'), 'Bonjour John Doe');
expectDeprecation(() => {
assert.equal(loc('_Hello %@', 'John'), 'Bonjour John');
assert.equal(loc('_Hello %@ %@', ['John'], 'Doe'), 'Bonjour John Doe');
}, /loc is deprecated/);
}
}
);

0 comments on commit c7526f1

Please sign in to comment.