forked from microsoft/BotFramework-Composer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: lg resolver with locale (microsoft#2362)
* fix lg resolver with locale * generate importResolver in shared lib
- Loading branch information
Showing
6 changed files
with
85 additions
and
84 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
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,51 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
export declare type ResolverResource = { content: string; id: string }; | ||
export declare type ImportResolverDelegate = (source: string, resourceId: string) => ResolverResource; | ||
|
||
function getFileName(path: string): string { | ||
return path.split('/').pop() || path; | ||
} | ||
|
||
/** | ||
* | ||
* @param resources resources feed to resolver | ||
* @param ext resource extension, e.g. .lg, .lu | ||
* @param defaultLocale complete resource id = [id].[locale][ext] | ||
*/ | ||
export function importResolverGenerator( | ||
resources: ResolverResource[], | ||
ext = '', | ||
defaultLocale = 'en-us' | ||
): ImportResolverDelegate { | ||
/** | ||
* @param source current file id | ||
* @param resourceId imported file id | ||
* for example: | ||
* in todosample.en-us.lg: | ||
* [import](../common/common.lg) | ||
* | ||
* would resolve to common.en-us.lg || common.lg | ||
* | ||
* source = todosample || todosample.en-us || todosample.en-us.lg || todosample.lg | ||
* resourceId = common || common.lg || ../common/common.lg | ||
* | ||
*/ | ||
return (source: string, resourceId: string) => { | ||
// eslint-disable-next-line security/detect-non-literal-regexp | ||
const extReg = new RegExp(ext + '$'); | ||
const sourceId = getFileName(source).replace(extReg, ''); | ||
const locale = sourceId.split('.').length > 1 ? sourceId.split('.').pop() : defaultLocale; | ||
const targetId = getFileName(resourceId).replace(extReg, ''); | ||
|
||
const targetFile = | ||
resources.find(({ id }) => id === `${targetId}.${locale}`) || resources.find(({ id }) => id === targetId); | ||
|
||
if (!targetFile) throw new Error(`file not found`); | ||
return { | ||
id: resourceId, | ||
content: targetFile.content, | ||
}; | ||
}; | ||
} |
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