-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '7.x' into backport/7.x/pr-74915
- Loading branch information
Showing
96 changed files
with
2,469 additions
and
579 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...dev/i18n/__snapshots__/utils.test.js.snap → ...8n/utils/__snapshots__/utils.test.js.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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, | ||
// classes | ||
ErrorReporter, // @ts-ignore | ||
} from './utils'; | ||
|
||
export { verifyICUMessage } from './verify_icu_message'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 malformed 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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.