@bablic-io/ sdk- core (英语)
服务器侧渲染的引擎 婴儿。它用您的 Bablic 项目的翻译翻译在服务器上翻译一页,并以访问者的语言返回完成的 HTML .
你需要一个Bablic账户和项目。 新到巴贝利克? 报名。 。 。 .
大多数网站需要适配器 :
- 下一部 Vercel 上的Js:
@bablic-io/sdk-vercel - 阿斯特罗:
@bablic-io/sdk-astro
直接使用此软件包支持另一个主机 。 要求节点.js 22.
安装
npm install @bablic-io/sdk-core
翻译一页
import { MemoryCache, translateDocument } from '@bablic-io/sdk-core';
const cache = new MemoryCache();
const result = await translateDocument({
html, // original-language HTML, including the Bablic script tag
url: 'https://example.com/fr-fr/pricing', // the translated URL being served (example)
cache,
});
if (result.status === 'complete') {
// result.html: translated page
// result.textMap: original → translated strings, for framework data
// result.links: your project's link rules and the links that changed
} else {
// 'skipped': not translated on the server (plan without server-side rendering, or the
// project doesn't translate this page); serve the original.
// 'failed': never serve or cache the result; see result.reason.
}
translateDocument 从页面的 Bablic 脚本标签读取您的工程,翻译服务器上的页面,并等待翻译完成.
为主机服务 : createPageHandler
请求流出适配器共享:路由,从同站取出原始页面,翻译,缓存,编辑绕行和失败规则.
import { createPageHandler, MemoryCache } from '@bablic-io/sdk-core';
const pages = createPageHandler({ project: 'P0000', cache: new MemoryCache() });
// In your request handler:
const response = await pages.handle(request); // undefined: not a translated URL
一个已经获取了原始页面的代理将它传递过来,然后得到对每个请求的答复:
const response = await pages.translateHtml({ request, html, status, headers });
路由、缓存、编辑器绕行和失败规则相同 handle; 原作从此不再被取出, 甚至对背景刷新也一样 。 未翻译的 URL , 或在服务器上已经翻译过的一页( U)x-moxie 页眉,或 <html data-moxie-mode="ssr">),回来时没有改变 x-moxie: pass 还有原因.
对于一个没有翻译的URL, await pages.explain(request) 说明为何(例如,项目有哪一种语言文件夹) x-moxie-reason 标题或日志行.
project 您的 Bablic 项目密钥 。 翻译的URL来自您的 Bablic 仪表板设置(语言文件夹或子域); projectResolver 财务报告和审定财务报表 routeFromConfig 单枪匹马就暴露出路线.
选项匹配适配器( E)project, (中文(简体) ), resolve, (中文(简体) ), pageTtl, (中文(简体) ), timeoutMs, (中文(简体) ), cache, (中文(简体) ), moxie, (中文(简体) ), editorCookie, (中文(简体) ), onResult, (中文(简体) ), onUnsupported, (中文(简体) ), host, (中文(简体) ), fetch),加上主机钩: buildId, (中文(简体) ), sourceHeaders, (中文(简体) ), ignoreParams, (中文(简体) ), dropRequestHeaders, (中文(简体) ), transformHtml 财务报告和审定财务报表 absoluteRedirects见 @bablic-io/sdk-vercel 读取 用于行为,响应头和托管规则.
其他出口
CacheStore: 缓存接口 (get, (中文(简体) ),set与ttl财务报告和审定财务报表tags, (中文(简体) ),expireTag) (中文(简体) ).MemoryCache执行它.createRevalidateHandler({ secret, cache }): 已签名的清理端点, 清除已缓存的页面, 或缓存的工程配置{"config": true}。 。 。 .rewriteHref(href, rules), (中文(简体) ),localizeHref(href, links): 将您的工程链接规则应用到 URL, 如 Bablic 在页面中所做的那样 .projectResolver({ project, cache }), (中文(简体) ),routeFromConfig(config, url): 您的项目翻译了 URL .isCrawler(userAgent), (中文(简体) ),isStagingUrl(url)。 。 。 .
链接
许可证
麻省理工学院