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 engine typescript migration #22441

Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
29e7b2a
configure typescript build, add necessary dependencies, change extens…
Aug 24, 2018
be079db
migrate lib files in root
Aug 24, 2018
f33965a
update tests snapshots, resolve core loader, helper
Aug 24, 2018
04261ee
fix types for core components
Aug 27, 2018
66f1e88
fix angular components
Aug 27, 2018
78edb2c
fix angular staff
Aug 27, 2018
ac8643c
use Messages type
Aug 27, 2018
277da23
first-upper-case letter while using classs
Aug 27, 2018
9dd7278
use stable latest babel, fix ts issues
Aug 28, 2018
c101364
optimize .babelrc
Aug 28, 2018
9c7d28f
update lock file
Aug 28, 2018
5a2e706
Merge branch 'master' into feature/i18n-engine-typescript-migration
maryia-lapata Sep 3, 2018
86601f1
Fix x-pack/yarn.lock
maryia-lapata Sep 3, 2018
2fdc1fc
Merge branch 'master' into feature/i18n-engine-typescript-migration
maryia-lapata Sep 4, 2018
8478477
Merge branch 'master' into feature/i18n-engine-typescript-migration
maryia-lapata Sep 6, 2018
61009ef
fix issue with unknown babel plugin
pavel06081991 Sep 12, 2018
f5a64cb
add babel-config.js file with babel configuration for i18n engine bui…
pavel06081991 Sep 12, 2018
5171c3d
Merge branch 'master' into HEAD
pavel06081991 Sep 17, 2018
678f7f5
Merge remote-tracking branch 'upstream/master' into feature/i18n-engi…
LeanidShutau Sep 24, 2018
bb238f0
Resolve comments
LeanidShutau Sep 25, 2018
1e5d32d
Fix babel config
LeanidShutau Sep 25, 2018
e5d911e
Fix packages incompatibility issue
LeanidShutau Sep 25, 2018
6c18014
Fix tslint errors
LeanidShutau Sep 25, 2018
d7cbdb6
Fix tests
LeanidShutau Sep 26, 2018
ab84eb8
Merge remote-tracking branch 'upstream/master' into feature/i18n-engi…
LeanidShutau Sep 27, 2018
264c8bb
Resolve comments
LeanidShutau Sep 27, 2018
9f04b31
Fix types
LeanidShutau Sep 28, 2018
c2479a7
Merge remote-tracking branch 'upstream/master' into feature/i18n-engi…
LeanidShutau Sep 28, 2018
7451a25
Merge remote-tracking branch 'upstream/master' into feature/i18n-engi…
LeanidShutau Oct 1, 2018
c9cf499
Merge branch 'master' into feature/i18n-engine-typescript-migration
maryia-lapata Oct 2, 2018
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,9 @@
"@kbn/eslint-plugin-license-header": "link:packages/kbn-eslint-plugin-license-header",
"@kbn/plugin-generator": "link:packages/kbn-plugin-generator",
"@kbn/test": "link:packages/kbn-test",
"@types/angular": "^1.6.50",
"@types/angular-mocks": "^1.7.0",
"@octokit/rest": "^15.10.0",
"@types/angular": "^1.6.45",
"@types/babel-core": "^6.25.5",
"@types/bluebird": "^3.1.1",
"@types/boom": "^7.2.0",
Expand Down
10 changes: 0 additions & 10 deletions packages/kbn-i18n/.babelrc

This file was deleted.

3 changes: 2 additions & 1 deletion packages/kbn-i18n/angular/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"browser": "../target/web/angular",
"main": "../target/node/angular"
"main": "../target/node/angular",
"types": "./target/types/angular/index.d.ts"
}
51 changes: 51 additions & 0 deletions packages/kbn-i18n/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/

// We can't use common Kibana presets here because of babel versions incompatibility
module.exports = {
plugins: ['@babel/plugin-proposal-class-properties', '@babel/plugin-proposal-object-rest-spread'],
presets: ['@babel/preset-react', '@babel/typescript'],
env: {
web: {
presets: [
[
'@babel/preset-env',
{
targets: {
browsers: ['last 2 versions', '> 5%', 'Safari 7'],
},
},
],
],
},
node: {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
},
},
ignore: ['**/*.test.ts', '**/*.test.tsx'],
};
30 changes: 20 additions & 10 deletions packages/kbn-i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,40 @@
"name": "@kbn/i18n",
"browser": "./target/web/browser.js",
"main": "./target/node/index.js",
"types": "./target/types/index.d.ts",
"module": "./src/index.js",
"version": "1.0.0",
"license": "Apache-2.0",
"private": true,
"scripts": {
"build": "yarn build:web && yarn build:node",
"build:web": "cross-env BABEL_ENV=web babel src --out-dir target/web",
"build:node": "cross-env BABEL_ENV=node babel src --out-dir target/node",
"build": "yarn build:web && yarn build:node && yarn build:types",
"build:types": "tsc --emitDeclarationOnly",
"build:web": "cross-env BABEL_ENV=web babel src --config-file ./babel.config.js --out-dir target/web --extensions \".ts,.js,.tsx\"",
"build:node": "cross-env BABEL_ENV=node babel src --config-file ./babel.config.js --out-dir target/node --extensions \".ts,.js,.tsx\"",
"kbn:bootstrap": "yarn build",
"kbn:watch": "yarn build --watch"
},
"devDependencies": {
"@kbn/babel-preset": "link:../kbn-babel-preset",
"@kbn/dev-utils": "link:../kbn-dev-utils",
"babel-cli": "^6.26.0",
"cross-env": "^5.2.0"
"@babel/cli": "^7.1.0",
"@babel/core": "^7.1.0",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.1.0",
"@types/intl-relativeformat": "^2.1.0",
"@types/json5": "^0.0.30",
"@types/react-intl": "^2.3.11",
"cross-env": "^5.2.0",
"typescript": "^3.0.3"
},
"dependencies": {
"intl-format-cache": "^2.1.0",
"intl-messageformat": "^2.2.0",
"intl-relativeformat": "^2.1.0",
"json5": "^1.0.1",
"prop-types": "^15.5.8",
"json5": "^2.0.1",
"prop-types": "^15.6.2",
"react": "^16.3.0",
"react-intl": "^2.4.0"
"react-intl": "^2.7.0"
}
}
3 changes: 2 additions & 1 deletion packages/kbn-i18n/react/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"browser": "../target/web/react",
"main": "../target/node/react"
"main": "../target/node/react",
"types": "./target/types/react/index.d.ts"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@
import angular from 'angular';
import 'angular-mocks';
import { i18nDirective } from './directive';
import { i18nProvider } from './provider';
import { I18nProvider } from './provider';

angular
.module('app', [])
.provider('i18n', i18nProvider)
.provider('i18n', I18nProvider)
.directive('i18nId', i18nDirective);

describe('i18nDirective', () => {
let compile;
let scope;
let compile: angular.ICompileService;
let scope: angular.IRootScopeService;

beforeEach(angular.mock.module('app'));
beforeEach(
angular.mock.inject(($compile, $rootScope) => {
compile = $compile;
scope = $rootScope.$new();
})
angular.mock.inject(
($compile: angular.ICompileService, $rootScope: angular.IRootScopeService) => {
compile = $compile;
scope = $rootScope.$new();
}
)
);

it('inserts correct translation html content', () => {
test('inserts correct translation html content', () => {
const id = 'id';
const defaultMessage = 'default-message';

Expand All @@ -56,7 +58,7 @@ describe('i18nDirective', () => {
expect(element.html()).toEqual(defaultMessage);
});

it('inserts correct translation html content with values', () => {
test('inserts correct translation html content with values', () => {
const id = 'id';
const defaultMessage = 'default-message {word}';
const compiledContent = 'default-message word';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,28 @@
* under the License.
*/

export function i18nDirective(i18n) {
import { I18nServiceType } from './provider';

export function i18nDirective(i18n: I18nServiceType) {
return {
restrict: 'A',
scope: {
id: '@i18nId',
defaultMessage: '@i18nDefaultMessage',
values: '=i18nValues',
},
link: function($scope, $element) {
$scope.$watchGroup(['id', 'defaultMessage', 'values'], function([
id,
defaultMessage = '',
values = {},
]) {
$element.html(
i18n(id, {
values,
defaultMessage,
})
);
});
link($scope: angular.IScope, $element: angular.IRootElementService) {
$scope.$watchGroup(
['id', 'defaultMessage', 'values'],
([id, defaultMessage = '', values = {}]) => {
$element.html(
i18n(id, {
values,
defaultMessage,
})
);
}
);
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@
* under the License.
*/

import angular from 'angular';
import 'angular-mocks';
import { i18nProvider } from './provider';
import { i18nFilter } from './filter';
import * as i18n from '../core/i18n';

jest.mock('../core/i18n', () => ({
translate: jest.fn().mockImplementation(() => 'translation'),
}));

import angular from 'angular';
import 'angular-mocks';

import * as i18n from '../core/i18n';
import { i18nFilter as angularI18nFilter } from './filter';
import { I18nProvider, I18nServiceType } from './provider';

angular
.module('app', [])
.provider('i18n', i18nProvider)
.filter('i18n', i18nFilter);
.provider('i18n', I18nProvider)
.filter('i18n', angularI18nFilter);

describe('i18nFilter', () => {
let filter;
let filter: I18nServiceType;

beforeEach(angular.mock.module('app'));
beforeEach(
Expand All @@ -45,15 +46,15 @@ describe('i18nFilter', () => {
jest.resetAllMocks();
});

it('provides wrapper around i18n engine', () => {
test('provides wrapper around i18n engine', () => {
const id = 'id';
const defaultMessage = 'default-message';
const values = {};

const result = filter(id, { defaultMessage, values });

expect(result).toEqual('translation');
expect(i18n.translate).toHaveBeenCalledTimes(1);
expect(i18n.translate).toHaveBeenCalledWith(id, { defaultMessage, values });
expect(result).toEqual('translation');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
* under the License.
*/

export function i18nFilter(i18n) {
return function(id, { defaultMessage = '', values = {} } = {}) {
import { I18nServiceType } from './provider';

export function i18nFilter(i18n: I18nServiceType) {
return (id: string, { defaultMessage = '', values = {} } = {}) => {
return i18n(id, {
values,
defaultMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
* under the License.
*/

export { i18nProvider } from './provider';
export { I18nProvider } from './provider';
export { i18nFilter } from './filter';
export { i18nDirective } from './directive';
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,37 @@

import angular from 'angular';
import 'angular-mocks';
import { i18nProvider } from './provider';
import * as i18n from '../core/i18n';

angular.module('app', []).provider('i18n', i18nProvider);
import * as i18nCore from '../core/i18n';
import { I18nProvider, I18nServiceType } from './provider';

angular.module('app', []).provider('i18n', I18nProvider);

describe('i18nProvider', () => {
let provider;
let service;
let provider: I18nProvider;
let service: I18nServiceType;

beforeEach(
angular.mock.module('app', [
'i18nProvider',
i18n => {
service = i18n;
(i18n: I18nProvider) => {
LeanidShutau marked this conversation as resolved.
Show resolved Hide resolved
provider = i18n;
},
])
);
beforeEach(
angular.mock.inject(i18n => {
provider = i18n;
angular.mock.inject((i18n: I18nServiceType) => {
service = i18n;
})
);

it('provides wrapper around i18n engine', () => {
expect(provider).toEqual(i18n.translate);
test('provides wrapper around i18n engine', () => {
expect(service).toEqual(i18nCore.translate);
});

it('provides service wrapper around i18n engine', () => {
const serviceMethodNames = Object.keys(service);
const pluginMethodNames = Object.keys(i18n);
test('provides service wrapper around i18n engine', () => {
const serviceMethodNames = Object.keys(provider);
const pluginMethodNames = Object.keys(i18nCore);

expect([...serviceMethodNames, 'translate'].sort()).toEqual(
[...pluginMethodNames, '$get'].sort()
Expand Down
36 changes: 36 additions & 0 deletions packages/kbn-i18n/src/angular/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 * as i18n from '../core';

export type I18nServiceType = ReturnType<I18nProvider['$get']>;

export class I18nProvider implements angular.IServiceProvider {
public addMessages = i18n.addMessages;
public getMessages = i18n.getMessages;
public setLocale = i18n.setLocale;
public getLocale = i18n.getLocale;
public setDefaultLocale = i18n.setDefaultLocale;
public getDefaultLocale = i18n.getDefaultLocale;
public setFormats = i18n.setFormats;
public getFormats = i18n.getFormats;
public getRegisteredLocales = i18n.getRegisteredLocales;
public init = i18n.init;
public $get = () => i18n.translate;
}
File renamed without changes.
Loading