-
Notifications
You must be signed in to change notification settings - Fork 28
Added i18n support to dbsearch plugin #74
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "admin.menuname":"DB Search", | ||
| "admin.title": "DB Search", | ||
| "admin.topicsIndexed": "Topics Indexed:", | ||
| "admin.postsIndexed": "Posts Indexed:", | ||
| "admin.messagesIndexed": "Messages Indexed:", | ||
| "admin.reindex": "Re Index", | ||
| "admin.clearIndex": "Clear Index", | ||
| "admin.working": "Working...", | ||
| "admin.indexLanguage": "Index Language", | ||
| "admin.changeLanguage": "Change Language", | ||
| "admin.topicLimit": "Topic Limit", | ||
| "admin.topicLimitPlaceholder": "Number of topics to return", | ||
| "admin.postLimit": "Post Limit", | ||
| "admin.postLimitPlaceholder": "Number of posts to return", | ||
| "admin.excludeCategories": "Select categories to exclude from indexing" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "admin.menuname":"数据库搜索", | ||
| "admin.title": "数据库搜索", | ||
| "admin.topicsIndexed": "已索引主题:", | ||
| "admin.postsIndexed": "已索引帖子:", | ||
| "admin.messagesIndexed": "已索引消息:", | ||
| "admin.reindex": "重新索引", | ||
| "admin.clearIndex": "清空索引", | ||
| "admin.working": "正在处理...", | ||
| "admin.indexLanguage": "索引语言", | ||
| "admin.changeLanguage": "更改语言", | ||
| "admin.topicLimit": "主题限制", | ||
| "admin.topicLimitPlaceholder": "要返回的主题数量", | ||
| "admin.postLimit": "帖子限制", | ||
| "admin.postLimitPlaceholder": "要返回的帖子数量", | ||
| "admin.excludeCategories": "选择要从索引中排除的分类" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ const batch = require.main.require('./src/batch'); | |
| const plugins = require.main.require('./src/plugins'); | ||
| const categories = require.main.require('./src/categories'); | ||
| const pubsub = require.main.require('./src/pubsub'); | ||
| var translator = require.main.require('./src/translator'); | ||
|
|
||
| const searchModule = require(`./${nconf.get('database')}`); | ||
|
|
||
|
|
@@ -38,6 +39,7 @@ const languageLookup = { | |
| es: 'spanish', | ||
| sv: 'swedish', | ||
| tr: 'turkish', | ||
| zh: 'chinese_zh', | ||
| }; | ||
|
|
||
| const defaultPostLimit = 500; | ||
|
|
@@ -53,7 +55,7 @@ const batchSize = 500; | |
|
|
||
| const search = module.exports; | ||
|
|
||
| function convertLanguageName(name) { | ||
| function convertLanguageName (name) { | ||
| if (nconf.get('database') === 'postgres') { | ||
| return languageLookup[name] || languageLookup.en; | ||
| } | ||
|
|
@@ -262,7 +264,7 @@ search.reindex = async function () { | |
| }); | ||
| }; | ||
|
|
||
| async function reIndexTopics() { | ||
| async function reIndexTopics () { | ||
| await batch.processSortedSet('topics:tid', async (tids) => { | ||
| const topicData = await topics.getTopicsFields(tids, ['tid', 'title', 'uid', 'cid', 'deleted', 'timestamp']); | ||
| await topicsSave(topicData); | ||
|
|
@@ -271,10 +273,10 @@ async function reIndexTopics() { | |
| }); | ||
| } | ||
|
|
||
| async function topicsSave(topics) { | ||
| async function topicsSave (topics) { | ||
| topics = topics.filter( | ||
| t => t && utils.isNumber(t.tid) && parseInt(t.deleted, 10) !== 1 && | ||
| !pluginConfig.excludeCategories.includes(String(t.cid)) | ||
| !pluginConfig.excludeCategories.includes(String(t.cid)) | ||
| ); | ||
|
|
||
| let data = topics.map((topicData) => { | ||
|
|
@@ -308,7 +310,7 @@ async function topicsSave(topics) { | |
| await db.incrObjectFieldBy('nodebb-plugin-dbsearch', 'topicsIndexed', result.tids.length); | ||
| } | ||
|
|
||
| async function reIndexPosts() { | ||
| async function reIndexPosts () { | ||
| await batch.processSortedSet('posts:pid', async (pids) => { | ||
| let postData = await posts.getPostsFields(pids, ['pid', 'content', 'uid', 'tid', 'deleted', 'timestamp']); | ||
| postData = postData.filter(p => p && p.deleted !== 1); | ||
|
|
@@ -327,10 +329,10 @@ async function reIndexPosts() { | |
| }); | ||
| } | ||
|
|
||
| async function postsSave(posts) { | ||
| async function postsSave (posts) { | ||
| posts = posts.filter( | ||
| p => p && utils.isNumber(p.pid) && parseInt(p.deleted, 10) !== 1 && | ||
| !pluginConfig.excludeCategories.includes(String(p.cid)) | ||
| !pluginConfig.excludeCategories.includes(String(p.cid)) | ||
| ); | ||
|
|
||
| let data = posts.map((postData) => { | ||
|
|
@@ -364,7 +366,7 @@ async function postsSave(posts) { | |
| await db.incrObjectFieldBy('nodebb-plugin-dbsearch', 'postsIndexed', result.pids.length); | ||
| } | ||
|
|
||
| async function reIndexMessages() { | ||
| async function reIndexMessages () { | ||
| await batch.processSortedSet(`messages:mid`, async (mids) => { | ||
| let messageData = await messaging.getMessagesFields(mids, ['mid', 'content', 'roomId', 'fromuid', 'deleted', 'system']); | ||
| messageData = messageData.filter(p => p && p.deleted !== 1 && p.system !== 1); | ||
|
|
@@ -374,7 +376,7 @@ async function reIndexMessages() { | |
| }); | ||
| } | ||
|
|
||
| async function messagesSave(msgs) { | ||
| async function messagesSave (msgs) { | ||
| msgs = msgs.filter(m => m && utils.isNumber(m.mid) && parseInt(m.deleted, 10) !== 1 && parseInt(m.system, 10) !== 1); | ||
|
|
||
| let data = msgs.map((msgData) => { | ||
|
|
@@ -405,7 +407,7 @@ async function messagesSave(msgs) { | |
| await db.incrObjectFieldBy('nodebb-plugin-dbsearch', 'messagesIndexed', result.mids.length); | ||
| } | ||
|
|
||
| async function searchRemove(key, ids) { | ||
| async function searchRemove (key, ids) { | ||
| ids = ids.filter(id => id && utils.isNumber(id)); | ||
| await db.searchRemove(key, ids); | ||
| if (key === 'topic') { | ||
|
|
@@ -417,7 +419,7 @@ async function searchRemove(key, ids) { | |
| } | ||
| } | ||
|
|
||
| async function reIndexTids(tids) { | ||
| async function reIndexTids (tids) { | ||
| if (!Array.isArray(tids) || !tids.length) { | ||
| return; | ||
| } | ||
|
|
@@ -428,11 +430,11 @@ async function reIndexTids(tids) { | |
| return; | ||
| } | ||
|
|
||
| async function reIndexTopicsPids(topicData) { | ||
| async function reIndexTopicsPids (topicData) { | ||
| await Promise.all(topicData.map(t => reIndexTopicPids(t))); | ||
| } | ||
|
|
||
| async function reIndexTopicPids(topic) { | ||
| async function reIndexTopicPids (topic) { | ||
| await reIndexPids([topic.mainPid], topic); | ||
| await batch.processSortedSet(`tid:${topic.tid}:posts`, async (pids) => { | ||
| await reIndexPids(pids, topic); | ||
|
|
@@ -447,7 +449,7 @@ async function reIndexTids(tids) { | |
| ]); | ||
| } | ||
|
|
||
| async function reIndexPids(pids, topic) { | ||
| async function reIndexPids (pids, topic) { | ||
| if (!Array.isArray(pids) || !pids.length) { | ||
| winston.warn('[nodebb-plugin-dbsearch] invalid-pid, skipping'); | ||
| return; | ||
|
|
@@ -464,14 +466,16 @@ async function reIndexPids(pids, topic) { | |
| await postsSave(postData); | ||
| } | ||
|
|
||
| async function renderAdmin(req, res) { | ||
| async function renderAdmin (req, res) { | ||
| const results = await getGlobalAndPluginData(); | ||
| results.plugin.progressData = await getProgress(); | ||
| results.plugin.title = 'DB Search'; | ||
| res.render('admin/plugins/dbsearch', results.plugin); | ||
| translator.translate('[[dbsearch:admin.title]]', function (translated) { | ||
| results.plugin.title = translated; | ||
| res.render('admin/plugins/dbsearch', results.plugin); | ||
| }); | ||
| } | ||
|
|
||
| async function save(req, res) { | ||
| async function save (req, res) { | ||
| if (utils.isNumber(req.body.postLimit) && utils.isNumber(req.body.topicLimit)) { | ||
| const data = { | ||
| postLimit: req.body.postLimit, | ||
|
|
@@ -494,7 +498,7 @@ socketAdmin.plugins.dbsearch.checkProgress = async function () { | |
| return await getProgress(); | ||
| }; | ||
|
|
||
| async function getPluginData() { | ||
| async function getPluginData () { | ||
| const data = await db.getObject('nodebb-plugin-dbsearch') || {}; | ||
| data.topicsIndexed = parseInt(data.topicsIndexed, 10) || 0; | ||
| data.postsIndexed = parseInt(data.postsIndexed, 10) || 0; | ||
|
|
@@ -514,7 +518,7 @@ async function getPluginData() { | |
| return data; | ||
| } | ||
|
|
||
| async function getGlobalAndPluginData() { | ||
| async function getGlobalAndPluginData () { | ||
| const [global, plugin, allCategories] = await Promise.all([ | ||
| db.getObjectFields('global', ['topicCount', 'postCount', 'messageCount']), | ||
| getPluginData(), | ||
|
|
@@ -552,7 +556,7 @@ async function getGlobalAndPluginData() { | |
| return { global: global, plugin: plugin, allCategories: allCategories }; | ||
| } | ||
|
|
||
| async function getProgress() { | ||
| async function getProgress () { | ||
| const [global, pluginData] = await Promise.all([ | ||
| db.getObjectFields('global', ['topicCount', 'postCount', 'messageCount']), | ||
| getPluginData(), | ||
|
|
@@ -595,7 +599,7 @@ socketAdmin.plugins.dbsearch.clearIndex = async function () { | |
| }, 0); | ||
| }; | ||
|
|
||
| async function clearIndex() { | ||
| async function clearIndex () { | ||
| await db.setObject('nodebb-plugin-dbsearch', { | ||
| working: 1, | ||
| }); | ||
|
|
@@ -614,7 +618,7 @@ async function clearIndex() { | |
| }); | ||
| } | ||
|
|
||
| async function clearSet(set, key) { | ||
| async function clearSet (set, key) { | ||
| await batch.processSortedSet(set, async (ids) => { | ||
| await searchRemove(key, ids); | ||
| }, { | ||
|
|
@@ -629,13 +633,14 @@ socketAdmin.plugins.dbsearch.changeLanguage = async function (socket, language) | |
|
|
||
| const admin = {}; | ||
| admin.menu = function (custom_header, callback) { | ||
| custom_header.plugins.push({ | ||
| route: '/plugins/dbsearch', | ||
| icon: 'fa-search', | ||
| name: 'DB Search', | ||
| translator.translate('[[dbsearch:admin.menuname]]', function (translated) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for manualy translation just set name to |
||
| custom_header.plugins.push({ | ||
| route: '/plugins/dbsearch', | ||
| icon: 'fa-search', | ||
| name: translated, | ||
| }); | ||
| callback(null, custom_header); | ||
| }); | ||
|
|
||
| callback(null, custom_header); | ||
| }; | ||
|
|
||
| search.admin = admin; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ const nconf = require.main.require('nconf'); | |
| const db = require.main.require('./src/database'); | ||
| const pubsub = require.main.require('./src/pubsub'); | ||
|
|
||
| let searchLanguage = 'english'; | ||
| let searchLanguage = 'chinese_zh'; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep the default as english |
||
|
|
||
| pubsub.on('dbsearch-language-changed', (e) => { | ||
| searchLanguage = e.data; | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| "license": "BSD-2-Clause", | ||
| "dependencies": { | ||
| "lodash": "4.17.21", | ||
| "nodebb-plugin-dbsearch": "file:", | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's this doing?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed it to a local reference because I placed the plugin code in the src/plugins directory of the NodeBB project and bundled it for testing using the file: method. You can easily revert it to the original NPM installation method—just restore the plugin version number in the package.json (or remove this line entirely to return to the default dependency), then reinstall it. |
||
| "redisearch": "^1.0.3" | ||
| }, | ||
| "devDependencies": { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't have to manually translate title I think, you can just set
results.plugin.title = '[[dbsearch:admin.title]]and it should be translated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK