想要一个单词表,于是决定使用下wordBook(安装了Translation插件才会有)(虽然一点也不好用,没法更新单词、不能搜索等),但是仍然可以将就用一下
就用它的自动导入吧
用json文件导入
php脚本:
<?php
// Read contents from a.json file
$jsonString = file_get_contents('a.json');
// Decode JSON string to array
$dataArray = json_decode($jsonString, true);
// Initialize variables
$newArray = [];
$id = 0;
// Loop through each item in the array and transform as needed
foreach ($dataArray as $key => $value) {
$newItem = [];
$newItem['id'] = ++$id;
// Convert camel case word to underscore-separated and add to new item
$newItem['word'] = camelToUnderscore($key);
$newItem['sourceLanguage'] = 'en';
$newItem['targetLanguage'] = 'zh';
$newItem['explanation'] = $value;
$newItem['tags'] = [];
$newItem['createdAt'] = 1682585254881;
$newArray[] = $newItem;
}
// Encode the new array to JSON and write it to c.json file
file_put_contents('c.json', json_encode($newArray, JSON_UNESCAPED_UNICODE));
/**
* Converts a camel case string to underscore separated string.
*
* @param string $camel The camel case string to convert.
* @return string The underscore separated string.
*/
function camelToUnderscore(string $camel): string
{
return strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $camel));
}
生成的导入文件长这样:
效果: