Skip to content
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

[Tools] Rename i18n context field to description field #25105

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/kbn-i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ when missing translations
For the detailed explanation, see the section below
- `getFormats()` - returns current formats
- `getRegisteredLocales()` - returns array of locales having translations
- `translate(id: string, [{values: object, defaultMessage: string, context: string}])` –
translate message by id. `context` is optional context comment that will be extracted
- `translate(id: string, [{values: object, defaultMessage: string, description: string}])` –
translate message by id. `description` is optional context comment that will be extracted
by i18n tools and added as a comment next to translation message at `defaultMessages.json`.
- `init(messages: Map<string, string>)` - initializes the engine

Expand Down Expand Up @@ -269,7 +269,7 @@ class RootComponent extends Component {
}
```

Optionally we can pass `context` prop into `FormattedMessage` component.
Optionally we can pass `description` prop into `FormattedMessage` component.
This prop is optional context comment that will be extracted by i18n tools
and added as a comment next to translation message at `defaultMessages.json`

Expand All @@ -292,6 +292,7 @@ const MyComponentContent = ({ intl }) => (
id: 'welcome',
defaultMessage: 'Hello {name}, you have {unreadCount, number}\
{unreadCount, plural, one {message} other {messages}}',
description: 'Message description',
},
{ name, unreadCount }
)}
Expand Down Expand Up @@ -370,7 +371,7 @@ Where:
- `values` - values to pass into translation
LeanidShutau marked this conversation as resolved.
Show resolved Hide resolved
- `defaultMessage` - will be used unless translation was successful (the final
fallback in english, will be used for generating `en.json`)
- `context` - optional context comment that will be extracted by i18n tools
- `description` - optional context comment that will be extracted by i18n tools
and added as a comment next to translation message at `defaultMessages.json`

The translation `directive` has the following syntax:
Expand All @@ -379,15 +380,15 @@ The translation `directive` has the following syntax:
i18n-id="{string}"
[i18n-values="{object}"]
[i18n-default-message="{string}"]
[i18n-context="{string}"]
[i18n-description="{string}"]
></ANY>
```

Where:
- `i18n-id` - translation id to be translated
- `i18n-values` - values to pass into translation
- `i18n-default-message` - will be used unless translation was successful
- `i18n-context` - optional context comment that will be extracted by i18n tools
- `i18n-description` - optional context comment that will be extracted by i18n tools
and added as a comment next to translation message at `defaultMessages.json`

Angular `I18n` module is placed into `autoload` module, so it will be
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-i18n/src/core/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export function getRegisteredLocales() {
interface TranslateArguments {
values?: { [key: string]: string | number | Date };
defaultMessage?: string;
context?: string;
description?: string;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ i18n('plugin_1.id_1', { defaultMessage: 'Message 1' });
// @kbn/i18n
i18n.translate('plugin_1.id_2', {
defaultMessage: 'Message 2',
context: 'Message context',
description: 'Message description',
});

// React component. FormattedMessage, Intl.formatMessage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ i18n('plugin_3.duplicate_id', { defaultMessage: 'Message 1' });

i18n.translate('plugin_3.duplicate_id', {
defaultMessage: 'Message 2',
context: 'Message context',
description: 'Message description',
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,49 @@ Array [
Array [
"plugin_1.id_1",
Object {
"context": undefined,
"description": undefined,
"message": "Message 1",
},
],
Array [
"plugin_1.id_2",
Object {
"context": "Message context",
"description": "Message description",
"message": "Message 2",
},
],
Array [
"plugin_1.id_3",
Object {
"context": undefined,
"description": undefined,
"message": "Message 3",
},
],
Array [
"plugin_1.id_4",
Object {
"context": undefined,
"description": undefined,
"message": "Message 4",
},
],
Array [
"plugin_1.id_5",
Object {
"context": undefined,
"description": undefined,
"message": "Message 5",
},
],
Array [
"plugin_1.id_6",
Object {
"context": "",
"description": "",
"message": "Message 6",
},
],
Array [
"plugin_1.id_7",
Object {
"context": undefined,
"description": undefined,
"message": "Message 7",
},
],
Expand Down
2 changes: 1 addition & 1 deletion src/dev/i18n/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
*/

export const DEFAULT_MESSAGE_KEY = 'defaultMessage';
export const CONTEXT_KEY = 'context';
export const DESCRIPTION_KEY = 'description';
export const VALUES_KEY = 'values';
6 changes: 3 additions & 3 deletions src/dev/i18n/extractors/__snapshots__/code.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ Array [
Array [
"kbn.mgmt.id-1",
Object {
"context": undefined,
"description": undefined,
"message": "Message text 1",
},
],
Array [
"kbn.mgmt.id-2",
Object {
"context": "Message context",
"description": "Message description",
"message": "Message text 2",
},
],
Array [
"kbn.mgmt.id-3",
Object {
"context": undefined,
"description": undefined,
"message": "Message text 3",
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Array [
Array [
"ui.id-1",
Object {
"context": "Message context",
"description": "Message description",
"message": "Message text",
},
],
Expand Down
8 changes: 4 additions & 4 deletions src/dev/i18n/extractors/__snapshots__/html.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ Array [
Array [
"kbn.dashboard.id-1",
Object {
"context": "Message context 1",
"description": "Message description 1",
"message": "Message text 1 {value}",
},
],
Array [
"kbn.dashboard.id-2",
Object {
"context": undefined,
"description": undefined,
"message": "Message text 2",
},
],
Array [
"kbn.dashboard.id-3",
Object {
"context": "Message context 3",
"description": "Message description 3",
"message": "Message text 3",
},
],
Expand All @@ -31,7 +31,7 @@ Array [
Array [
"kbn.id",
Object {
"context": undefined,
"description": undefined,
"message": "Message text with {value}",
},
],
Expand Down
8 changes: 4 additions & 4 deletions src/dev/i18n/extractors/__snapshots__/i18n_call.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`dev/i18n/extractors/i18n_call extracts "i18n" and "i18n.translate" func
Array [
"message-id-1",
Object {
"context": "Message context 1",
"description": "Message description 1",
"message": "Default message 1",
},
]
Expand All @@ -14,7 +14,7 @@ exports[`dev/i18n/extractors/i18n_call extracts "i18n" and "i18n.translate" func
Array [
"message-id-2",
Object {
"context": "Message context 2",
"description": "Message description 2",
"message": "Default message 2",
},
]
Expand All @@ -24,8 +24,8 @@ exports[`dev/i18n/extractors/i18n_call extracts "i18n" and "i18n.translate" func
Array [
"message-id-3",
Object {
"context": "Message
context 3",
"description": "Message
description 3",
"message": "Default
message 3",
},
Expand Down
4 changes: 2 additions & 2 deletions src/dev/i18n/extractors/__snapshots__/pug.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`dev/i18n/extractors/pug extracts messages from pug template with interp
Array [
"message-id",
Object {
"context": "Message context",
"description": "Message description",
"message": "Default message",
},
]
Expand All @@ -14,7 +14,7 @@ exports[`dev/i18n/extractors/pug extracts messages from pug template without int
Array [
"message-id",
Object {
"context": "Message context",
"description": "Message description",
"message": "Default message",
},
]
Expand Down
6 changes: 3 additions & 3 deletions src/dev/i18n/extractors/__snapshots__/react.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`dev/i18n/extractors/react extractFormattedMessages extracts messages fr
Array [
"message-id-2",
Object {
"context": "Message context 2",
"description": "Message description 2",
"message": "Default message 2",
},
]
Expand All @@ -14,13 +14,13 @@ exports[`dev/i18n/extractors/react extractIntlMessages extracts messages from "i
Array [
"message-id-1",
Object {
"context": "Message context 1",
"description": "Message description 1",
"message": "Default message 1",
},
]
`;

exports[`dev/i18n/extractors/react extractIntlMessages throws if context value is not a string literal 1`] = `"context value should be a string or template literal (\\"message-id\\")."`;
exports[`dev/i18n/extractors/react extractIntlMessages throws if description value is not a string literal 1`] = `"description value should be a string or template literal (\\"message-id\\")."`;

exports[`dev/i18n/extractors/react extractIntlMessages throws if defaultMessage value is not a string literal 1`] = `"defaultMessage value should be a string or template literal (\\"message-id\\")."`;

Expand Down
12 changes: 6 additions & 6 deletions src/dev/i18n/extractors/code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Component extends PureComponent {
<FormattedMessage
id="kbn.mgmt.id-2"
defaultMessage="Message text 2"
context="Message context"
description="Message description"
/>
{intl.formatMessage({ id: 'kbn.mgmt.id-3', defaultMessage: 'Message text 3' })}
</div>
Expand All @@ -47,10 +47,10 @@ class Component extends PureComponent {
`);

const intlFormatMessageSource = `
formatMessage({ id: 'kbn.mgmt.id-1', defaultMessage: 'Message text 1', context: 'Message context' });
intl.formatMessage({ id: 'kbn.mgmt.id-2', defaultMessage: 'Message text 2', context: 'Message context' });
props.intl.formatMessage({ id: 'kbn.mgmt.id-5', defaultMessage: 'Message text 5', context: 'Message context' });
this.props.intl.formatMessage({ id: 'kbn.mgmt.id-6', defaultMessage: 'Message text 6', context: 'Message context' });
formatMessage({ id: 'kbn.mgmt.id-1', defaultMessage: 'Message text 1', description: 'Message description' });
intl.formatMessage({ id: 'kbn.mgmt.id-2', defaultMessage: 'Message text 2', description: 'Message description' });
props.intl.formatMessage({ id: 'kbn.mgmt.id-5', defaultMessage: 'Message text 5', description: 'Message description' });
this.props.intl.formatMessage({ id: 'kbn.mgmt.id-6', defaultMessage: 'Message text 6', description: 'Message description' });
`;

const formattedMessageSource = `
Expand All @@ -59,7 +59,7 @@ function f() {
<FormattedMessage
id="kbn.mgmt.id-1"
defaultMessage="Message text 1"
context="Message context"
description="Message description"
/>
);
}
Expand Down
11 changes: 6 additions & 5 deletions src/dev/i18n/extractors/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { formatJSString, checkValuesProperty } from '../utils';
import { createFailError } from '../../run';
import { DEFAULT_MESSAGE_KEY, DESCRIPTION_KEY } from '../constants';

const HBS_REGEX = /(?<=\{\{)([\s\S]*?)(?=\}\})/g;
const TOKENS_REGEX = /[^'\s]+|(?:'([^'\\]|\\[\s\S])*')/g;
Expand Down Expand Up @@ -64,14 +65,14 @@ export function* extractHandlebarsMessages(buffer) {
);
}

if (properties.context != null && typeof properties.context !== 'string') {
if (properties[DESCRIPTION_KEY] != null && typeof properties[DESCRIPTION_KEY] !== 'string') {
throw createFailError(
`Context value in Handlebars i18n should be a string ("${messageId}").`
`Description value in Handlebars i18n should be a string ("${messageId}").`
);
}

const message = formatJSString(properties.defaultMessage);
const context = formatJSString(properties.context);
const message = formatJSString(properties[DEFAULT_MESSAGE_KEY]);
const description = formatJSString(properties[DESCRIPTION_KEY]);

if (!message) {
throw createFailError(
Expand All @@ -89,6 +90,6 @@ export function* extractHandlebarsMessages(buffer) {

checkValuesProperty(Object.keys(valuesObject || {}), message, messageId);

yield [messageId, { message, context }];
yield [messageId, { message, description }];
}
}
6 changes: 3 additions & 3 deletions src/dev/i18n/extractors/handlebars.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ window.onload = function () {
var err = document.createElement('h1');
err.style['color'] = 'white';
err.innerText = '{{i18n 'ui.id-1' \
'{"defaultMessage": "Message text", "context": "Message context"}'}}';
'{"defaultMessage": "Message text", "description": "Message description"}'}}';

document.body.innerHTML = err.outerHTML;
}
Expand Down Expand Up @@ -65,7 +65,7 @@ window.onload = function () {
test('throws on empty id', () => {
const source = Buffer.from(`\
window.onload = function () {
err.innerText = '{{i18n '' '{"defaultMessage": "Message text", "context": "Message context"}'}}';
err.innerText = '{{i18n '' '{"defaultMessage": "Message text", "description": "Message description"}'}}';
};
`);

Expand All @@ -75,7 +75,7 @@ window.onload = function () {
test('throws on missing defaultMessage property', () => {
const source = Buffer.from(`\
window.onload = function () {
err.innerText = '{{i18n 'message-id' '{"context": "Message context"}'}}';
err.innerText = '{{i18n 'message-id' '{"description": "Message description"}'}}';
};
`);

Expand Down
Loading