跳至內容
Astera Technology

語言列轉欄位,額外語言也保留

兩種指定語言須齊備,其他語言另存。

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

來源格式與預期記錄

這是原創假設轉換例子。輸入限下列欄位的普通、無循環 JSON 形記錄,統一記錄格式及拒絕代碼由本例訂明。若換來源,先修改格式約定。

每列要有非空 locale、text,同一 locale 不能重複。en、zh-HK 必須都有,分別放入 en、zhHK。其他語言列保留在 unmapped。函數只搬文字,不能證明兩種語言意思一致。

可修改的假設轉換個案
個案輸入記錄(JSON)預期輸出(JSON)
整理已提供雙語{"entries":[{"locale":"en","text":"Front"},{"locale":"zh-HK","text":"前台"}]}{"ok":true,"value":{"en":"Front","zhHK":"前台","unmapped":[]}}
拒絕重複語言{"entries":[{"locale":"en","text":"Front"},{"locale":"en","text":"Desk"},{"locale":"zh-HK","text":"前台"}]}{"ok":false,"error":"LOCALE_DUPLICATE"}
保留其他語言{"entries":[{"locale":"en","text":"Front"},{"locale":"zh-HK","text":"前台"},{"locale":"fr","text":"Bureau"}]}{"ok":true,"value":{"en":"Front","zhHK":"前台","unmapped":[{"locale":"fr","text":"Bureau"}]}}
下載可編輯 CSV ↓

在本機執行轉換

把完整例子以 JavaScript 模組在 Node.js 執行,內含純轉換函數、三個輸入及寫明的預期輸出,對不上便由斷言停止。拒絕個案回傳 ok=false 及代碼,不回傳半份統一記錄。

完整函數及可執行個案
import assert from 'node:assert/strict';

function localisedLabels(i){
  const fail=error=>({ok:false,error});if(!i||!Array.isArray(i.entries))return fail('LOCALE_ENTRIES');
  const found=new Map(),unmapped=[];
  for(const e of i.entries){
    if(!e||typeof e.locale!=='string'||!e.locale||typeof e.text!=='string'||!e.text)return fail('LOCALE_VALUE');
    if(found.has(e.locale))return fail('LOCALE_DUPLICATE');found.set(e.locale,e.text);
    if(!['en','zh-HK'].includes(e.locale))unmapped.push({locale:e.locale,text:e.text});
  }
  if(!found.has('en')||!found.has('zh-HK'))return fail('LOCALE_MISSING');
  return {ok:true,value:{en:found.get('en'),zhHK:found.get('zh-HK'),unmapped}};
}

// Original synthetic cases; expected outputs are literal records.
const cases = [
  {
    "label": "Pair supplied locale text",
    "input": {
      "entries": [
        {
          "locale": "en",
          "text": "Front"
        },
        {
          "locale": "zh-HK",
          "text": "前台"
        }
      ]
    },
    "expected": {
      "ok": true,
      "value": {
        "en": "Front",
        "zhHK": "前台",
        "unmapped": []
      }
    }
  },
  {
    "label": "Reject duplicate locale",
    "input": {
      "entries": [
        {
          "locale": "en",
          "text": "Front"
        },
        {
          "locale": "en",
          "text": "Desk"
        },
        {
          "locale": "zh-HK",
          "text": "前台"
        }
      ]
    },
    "expected": {
      "ok": false,
      "error": "LOCALE_DUPLICATE"
    }
  },
  {
    "label": "Preserve another locale",
    "input": {
      "entries": [
        {
          "locale": "en",
          "text": "Front"
        },
        {
          "locale": "zh-HK",
          "text": "前台"
        },
        {
          "locale": "fr",
          "text": "Bureau"
        }
      ]
    },
    "expected": {
      "ok": true,
      "value": {
        "en": "Front",
        "zhHK": "前台",
        "unmapped": [
          {
            "locale": "fr",
            "text": "Bureau"
          }
        ]
      }
    }
  }
];
for (const sample of cases) {
  const actual = localisedLabels(sample.input);
  assert.deepEqual(actual, sample.expected);
  console.log(sample.label, JSON.stringify(actual));
}
下載可執行例子 ↓

判清邊界個案

保留個案中的法文 Bureau 怎樣處理?

查看參考答案

以 {locale:"fr",text:"Bureau"} 放在 unmapped,與 Front、前台並存。不覆蓋其他語言,也不能代替必需的 zh-HK。

相關資源