跳至內容
Astera Technology

生成文件時套正確語言設定

字樣及標點沿語言設定改,來源數量照留。

下載完整個案資料及參考答案 ↓

訂判斷及所需輸入

題內英語及香港中文設定各自訂字樣、冒號空格及單位標點。中文零數量文件須顯示「合共:0 件。」,lang 亦為 zh-HK。只改語言屬性、文字仍英文,未算套對設定。

已填本地測試工作表
情況輸入 JSON
原例測試{"locale":"zh-HK","quantity":0}
下載可編輯 CSV ↓

跑所附比較

程序核真正文字、文章語言及未改數量。兩套字樣由本例提供,不聲稱自動翻譯已等價。

錯誤及所需輸出
路徑輸出 JSON
錯誤路徑{"lang":"zh-HK","text":"Total: 0 units.","quantity":"0"}
所需路徑{"lang":"zh-HK","text":"合共:0 件。","quantity":"0"}
下載可編輯 CSV ↓
Node24.14.1 程序;須 Playwright1.63 Chromium
import assert from 'node:assert/strict';
const smallWhole=(x,max)=>Number.isSafeInteger(x)&&x>=0&&x<=max;
import {createRequire} from 'node:module';
const htmlEscape=s=>String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
async function withPage(action,options={}){
  const require=createRequire(import.meta.url);const {chromium}=require('playwright');
  const browser=await chromium.launch({headless:true});
  try{const context=await browser.newContext(options);const page=await context.newPage();
    const requests=[];await page.route('**/*',route=>{requests.push(route.request().url());return route.abort();});
    return await action(page,context,requests);
  }finally{await browser.close();}
}

const run=async function run(i,corrected=true){
    const profiles={en:{label:'Total',colon:': ',unit:' units.'},'zh-HK':{label:'合共',colon:':',unit:' 件。'}};
    if(!Object.hasOwn(profiles,i.locale))return {held:true,reason:'profile-missing'};
    if(!smallWhole(i.quantity,1000))return {held:true,reason:'quantity-missing-or-invalid'};
    return withPage(async page=>{const p=profiles[corrected?i.locale:'en'];await page.setContent('<article lang="'+i.locale+'"><p>'+p.label+p.colon+i.quantity+p.unit+'</p><data value="'+i.quantity+'"></data></article>');
      return page.evaluate(()=>({lang:document.querySelector('article').lang,text:document.querySelector('p').textContent,quantity:document.querySelector('data').value}));});
  };
const cases=[{"input":{"locale":"zh-HK","quantity":0},"corrected":false,"expected":{"lang":"zh-HK","text":"Total: 0 units.","quantity":"0"}},{"input":{"locale":"zh-HK","quantity":0},"corrected":true,"expected":{"lang":"zh-HK","text":"合共:0 件。","quantity":"0"}},{"input":{"locale":"en","quantity":3},"corrected":true,"expected":{"lang":"en","text":"Total: 3 units.","quantity":"3"}},{"input":{"locale":"fr","quantity":3},"corrected":true,"expected":{"held":true,"reason":"profile-missing"}}];
for(const test of cases){const actual=await run(test.input,test.corrected);assert.deepEqual(actual,test.expected);console.log(actual);}
下載可執行例子 ↓

按邊界依據作判斷

英文三件呈 Total: 3 units.;題內沒有法文設定,故暫停,不靜靜轉英文。

  1. 選所附語言設定
  2. 呈現字樣與數量
  3. 核真正文字及語言

所需語言沒有設定便暫停。

可編輯邊界輸入及結果
條件輸入 JSON預期輸出 JSON
沿英文設定{"locale":"en","quantity":3}{"lang":"en","text":"Total: 3 units.","quantity":"3"}
未有法文設定{"locale":"fr","quantity":3}{"held":true,"reason":"profile-missing"}
下載可編輯 CSV ↓

原例只設 lang=zh-HK,文件就完成了嗎?

查看參考答案

不是,真正字樣及標點也須沿中文設定;兩版數量都維持零。

相關資源