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

[I18n] verify select icu-message options are in english #74963

Merged
merged 12 commits into from
Aug 13, 2020
46 changes: 46 additions & 0 deletions src/dev/i18n/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export {
// constants
readFileAsync,
writeFileAsync,
makeDirAsync,
accessAsync,
globAsync,
// functions
normalizePath,
difference,
isPropertyWithKey,
isI18nTranslateFunction,
node,
formatJSString,
formatHTMLString,
traverseNodes,
createParserErrorMessage,
checkValuesProperty,
extractValueReferencesFromMessage,
extractMessageIdFromNode,
extractMessageValueFromNode,
extractDescriptionValueFromNode,
extractValuesKeysFromNode,
arrayify, // @ts-ignore
} from './utils';

export { verifyICUMessage } from './verify_icu_message';
41 changes: 41 additions & 0 deletions src/dev/i18n/utils/intl_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export interface OptionalFormatPatternNode {
type: 'optionalFormatPattern';
selector: string;
value: any;
}

export interface LinePosition {
offset: number;
line: number;
column: number;
}

export interface LocationNode {
start: LinePosition;
end: LinePosition;
}

export interface SelectFormatNode {
type: 'selectFormat';
options: OptionalFormatPatternNode[];
location: LocationNode;
}
22 changes: 0 additions & 22 deletions src/dev/i18n/utils.js → src/dev/i18n/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,28 +208,6 @@ export function checkValuesProperty(prefixedValuesKeys, defaultMessage, messageI
}
}

/**
* Verifies valid ICU message.
* @param message ICU message.
* @param messageId ICU message id
* @returns {undefined}
*/
export function verifyICUMessage(message) {
try {
parser.parse(message);
} catch (error) {
if (error.name === 'SyntaxError') {
const errorWithContext = createParserErrorMessage(message, {
loc: {
line: error.location.start.line,
column: error.location.start.column - 1,
},
message: error.message,
});
throw errorWithContext;
}
}
}
/**
* Extracts value references from the ICU message.
* @param message ICU message.
Expand Down
File renamed without changes.
91 changes: 91 additions & 0 deletions src/dev/i18n/utils/verify_icu_message.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { verifyICUMessage, checkEnglishOnly } from './verify_icu_message';

describe('verifyICUMessage', () => {
it('passes on plain text', () => {
const message = 'plain text here';
expect(() => verifyICUMessage(message)).not.toThrowError();
});

it('passes on empty string', () => {
const message = '';
expect(() => verifyICUMessage(message)).not.toThrowError();
});

it('passes on variable icu-syntax', () => {
const message = 'Your regular {foobar}';
expect(() => verifyICUMessage(message)).not.toThrowError();
});

it('passes on correct plural icu-syntax', () => {
const message = `You have {itemCount, plural,
=0 {no items}
one {1 item}
other {{itemCount} items}
}.`;

expect(() => verifyICUMessage(message)).not.toThrowError();
});

it('throws on malrformed string', () => {
const message =
'CDATA[extended_bounds設定を使用すると、強制的にヒストグラムアグリゲーションを実行し、特定の最小値に対してバケットの作成を開始し、最大値までバケットを作成し続けます。 ]]></target>\n\t\t\t<note>Kibana-SW - String "data.search.aggs.buckets.dateHistogram.extendedBounds.help" in Json.Root "messages\\strings" ';

expect(() => verifyICUMessage(message)).toThrowError();
});

it('throws on missing curly brackets', () => {
const message = `A missing {curly`;

expect(() => verifyICUMessage(message)).toThrowError();
});

it('throws on incorrect plural icu-syntax', () => {
// Notice that small/Medium/Large constants are swapped with the translation strings.
const message =
'{textScale, select, small {小さい} 中くらい {Medium} 大きい {Large} その他の {{textScale}} }';

expect(() => verifyICUMessage(message)).toThrowError();
});
});

describe('checkEnglishOnly', () => {
it('returns true on english only message', () => {
const result = checkEnglishOnly('english');

expect(result).toEqual(true);
});
it('returns true on empty message', () => {
const result = checkEnglishOnly('');

expect(result).toEqual(true);
});
it('returns false on message containing numbers', () => {
const result = checkEnglishOnly('english 123');

expect(result).toEqual(false);
});
it('returns false on message containing non-english alphabets', () => {
const result = checkEnglishOnly('i am 大きい');

expect(result).toEqual(false);
});
});
74 changes: 74 additions & 0 deletions src/dev/i18n/utils/verify_icu_message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// @ts-ignore
import parser from 'intl-messageformat-parser';
// @ts-ignore
import { createParserErrorMessage, traverseNodes } from './utils';
import { SelectFormatNode } from './intl_types';

export function checkEnglishOnly(message: string) {
return /^[a-z]*$/i.test(message);
}

export function verifySelectFormatNode(node: SelectFormatNode) {
if (node.type !== 'selectFormat') {
throw new parser.SyntaxError(
'Unable to verify select format icu-syntax',
'selectFormat',
node.type,
node.location
);
}

for (const option of node.options) {
if (option.type === 'optionalFormatPattern') {
if (!checkEnglishOnly(option.selector)) {
throw new parser.SyntaxError(
'selectFormat Selector must be in english',
'English only selector',
option.selector,
node.location
);
}
}
}
}

export function verifyICUMessage(message: string) {
try {
const results = parser.parse(message);
for (const node of results.elements) {
if (node.type === 'argumentElement' && node.format?.type === 'selectFormat') {
verifySelectFormatNode(node.format);
}
}
} catch (error) {
if (error.name === 'SyntaxError') {
const errorWithContext = createParserErrorMessage(message, {
loc: {
line: error.location.start.line,
column: error.location.start.column - 1,
},
message: error.message,
});
throw errorWithContext;
}
}
}