Skip to content

多语言预览集成 #83

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions packages/hap-server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { launch, stop } from './server'
import { remotePreview } from './preview/remote-preview'
import { beforeStart, createPreview } from './preview'

let globalCWD = ''

/**
* 启动开发服务
*
Expand Down Expand Up @@ -36,6 +38,7 @@ import { beforeStart, createPreview } from './preview'
function launchServer(options) {
try {
colorconsole.attach(options.log)
globalCWD = options.cwd
Object.assign(config.options, options)
// 配置参数
setCustomConfig(options.cwd, options.port)
Expand All @@ -47,6 +50,10 @@ function launchServer(options) {
return launch(config)
}

export function getCWD() {
return globalCWD
}

/**
* 关闭开发服务
*
Expand Down
11 changes: 10 additions & 1 deletion packages/hap-server/src/preview/create-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { KnownError, getLaunchPage, logger, eventBus } from '@hap-toolkit/shared

import { renderPage, trimSlash, removeAnsiColor } from './shared'

import { getCWD } from './../index'

const { PACKAGER_BUILD_DONE, PACKAGER_WATCH_START, PACKAGER_BUILD_PROGRESS } = eventBus

// 不存在也返回200文件
Expand Down Expand Up @@ -215,14 +217,21 @@ export default async function createRouter(previewTarget) {
const widgets = manifest.router.widgets || {}
const type = requestRoute in widgets ? 'card' : 'app'
const script = routes[requestRoute]
const currentLanguage = JSON.parse(process.env.VSCODE_NLS_CONFIG || `{locale: 'zh-CN'}`).locale;
const cwd = getCWD()

const modifiedPath = cwd.replace(':', ':\\')
const finalPath = modifiedPath.replace(/\\/g, '\\\\');
const html = await renderPage(TPL_PAGE_PATH, {
title: manifest.name,
routeName: requestRoute,
routes: JSON.stringify(routes),
type,
script,
scriptNotFound: !scriptExists(script),
webJsUrl: genWebJsUrl(ctx.conf.options.webVersion)
webJsUrl: genWebJsUrl(ctx.conf.options.webVersion),
language: currentLanguage,
i18nPath: finalPath
})
ctx.type = 'text/html'
ctx.body = html
Expand Down
4 changes: 4 additions & 0 deletions packages/hap-server/src/preview/views/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
z-index: 1002;
}
</style>
<script>
window.i18nPath = `{{i18nPath}}`
window.language = `{{language}}`
</script>
<script src="hap-preview://index.js"></script>
<!-- https://statres.quickapp.cn/quickapp/ide/web.js -->
<script src="{{webJsUrl}}"></script>
Expand Down