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

fix: lg resolver with locale #2362

Merged
merged 3 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions Composer/packages/client/src/store/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ImportResolverDelegate, LGParser } from 'botbuilder-lg';
import { LgFile, LuFile } from '@bfc/indexers';

import { prepareAxios } from '../utils/auth';
import { getFileName, getBaseName, getExtension } from '../utils/fileUtil';
import { getFileName } from '../utils/fileUtil';

import { reducer } from './reducer';
import bindActions from './action/bindActions';
Expand Down Expand Up @@ -122,13 +122,13 @@ export const StoreProvider: React.FC<StoreProviderProps> = props => {
dispatch: interceptDispatch,
resolvers: {
lgImportresolver: function(source: string, id: string) {
const locale = getExtension(source);
const targetFileName = getFileName(id);
let targetFileId = getBaseName(targetFileName);
if (locale) {
targetFileId += `.${locale}`;
}
const targetFile = getState().lgFiles.find(({ id }) => id === targetFileId);
const sourceId = getFileName(source).replace(/\.lg$/, '');
const locale = sourceId.split('.').length > 1 ? sourceId.split('.').pop() : 'en-us';
const targetId = getFileName(id).replace(/\.lg$/, '');

const targetFile =
getState().lgFiles.find(({ id }) => id === `${targetId}.${locale}`) ||
getState().lgFiles.find(({ id }) => id === targetId);
if (!targetFile) throw new Error(`${id} lg file not found`);
return { id, content: targetFile.content };
} as ImportResolverDelegate,
Expand Down
16 changes: 8 additions & 8 deletions Composer/packages/client/src/store/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ImportResolverDelegate } from 'botbuilder-lg';
import { ActionTypes, FileTypes, BotStatus } from '../../constants';
import { DialogSetting, ReducerFunc } from '../types';
import { UserTokenPayload } from '../action/types';
import { getExtension, getFileName, getBaseName } from '../../utils';
import { getExtension, getFileName } from '../../utils';
import settingStorage from '../../utils/dialogSettingStorage';
import luFileStatusStorage from '../../utils/luFileStatusStorage';
import { getReferredFiles } from '../../utils/luUtil';
Expand Down Expand Up @@ -147,13 +147,13 @@ const updateLgTemplate: ReducerFunc = (state, { id, content }) => {
return lgFile;
});
const lgImportresolver: ImportResolverDelegate = function(source: string, id: string) {
const locale = getExtension(source);
const targetFileName = getFileName(id);
let targetFileId = getBaseName(targetFileName);
if (locale) {
targetFileId += `.${locale}`;
}
const targetFile = lgFiles.find(({ id }) => id === targetFileId);
const sourceId = getFileName(source).replace(/\.lg$/, '');
const locale = sourceId.split('.').length > 1 ? sourceId.split('.').pop() : 'en-us';
const targetId = getFileName(id).replace(/\.lg$/, '');

const targetFile =
lgFiles.find(({ id }) => id === `${targetId}.${locale}`) || lgFiles.find(({ id }) => id === targetId);

if (!targetFile) throw new Error(`file not found`);
return { id, content: targetFile.content };
};
Expand Down
20 changes: 11 additions & 9 deletions Composer/packages/server/src/models/bot/botProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,18 +528,20 @@ export class BotProject {
* in todosample.en-us.lg:
* [import](../common/common.lg)
*
* resolve to common.en-us.lg
* resolve to common.en-us.lg || common.lg
*
* source = todosample || todosample.en-us || todosample.en-us.lg || todosample.lg
* id = common || common.lg || ../common/common.lg
*
* source = todosample.en-us || AddToDo
* id = ../common/common.lg || common.lg || common
*/
private _lgImportResolver = (source: string, id: string) => {
const locale = source.split('.').length > 1 ? source.split('.').pop() : '';
let targetId = Path.basename(id, '.lg');
if (locale) {
targetId += `.${locale}`;
}
const targetFile = this.lgFiles.find(({ id }) => id === targetId);
const sourceId = Path.basename(source, '.lg');
const locale = sourceId.split('.').length > 1 ? sourceId.split('.').pop() : this.locale;
const targetId = Path.basename(id, '.lg');

const targetFile =
this.files.find(({ name }) => name === `${targetId}.${locale}.lg`) ||
this.files.find(({ name }) => name === `${targetId}.lg`);
if (!targetFile) throw new Error('file not found');
return {
id,
Expand Down
40 changes: 24 additions & 16 deletions Composer/packages/server/src/services/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ export class BotProjectService {

public static lgImportResolver(source: string, id: string, projectId: string): TextFile {
BotProjectService.initialize();
let targetId = Path.basename(id, '.lg');
if (targetId.lastIndexOf('.') === -1) {
const locale = source.lastIndexOf('.') > 0 ? source.split('.').pop() : 'en-us';
targetId += `.${locale}`;
}
const targetFile = BotProjectService.currentBotProjects
.find(({ id }) => id === projectId)
?.lgFiles.find(({ id }) => id === targetId);
const sourceId = Path.basename(source, '.lg');
const locale = sourceId.split('.').length > 1 ? sourceId.split('.').pop() : 'en-us';
const targetId = Path.basename(id, '.lg');

const project = BotProjectService.currentBotProjects.find(({ id }) => id === projectId);

if (!project) throw new Error('project not found');

const targetFile =
project.lgFiles.find(({ id }) => id === `${targetId}.${locale}`) ||
project.lgFiles.find(({ id }) => id === targetId);

if (!targetFile) throw new Error('lg file not found');
return {
id,
Expand All @@ -52,14 +56,18 @@ export class BotProjectService {

public static luImportResolver(source: string, id: string, projectId: string): any {
BotProjectService.initialize();
let targetId = Path.basename(id, '.lu');
if (targetId.lastIndexOf('.') === -1) {
const locale = source.lastIndexOf('.') > 0 ? source.split('.').pop() : 'en-us';
targetId += `.${locale}`;
}
const targetFile = BotProjectService.currentBotProjects
.find(({ id }) => id === projectId)
?.luFiles.find(({ id }) => id === targetId);
const sourceId = Path.basename(source, '.lu');
const locale = sourceId.split('.').length > 1 ? sourceId.split('.').pop() : 'en-us';
const targetId = Path.basename(id, '.lu');

const project = BotProjectService.currentBotProjects.find(({ id }) => id === projectId);

if (!project) throw new Error('project not found');

const targetFile =
project.luFiles.find(({ id }) => id === `${targetId}.${locale}`) ||
project.luFiles.find(({ id }) => id === targetId);

if (!targetFile) throw new Error('lu file not found');
return {
id,
Expand Down