feat: auto detect language

This commit is contained in:
hanyixuanten
2026-07-31 17:07:34 +08:00
parent ea65c7b93b
commit 460a70fc3c
10 changed files with 63 additions and 30 deletions
+3 -1
View File
@@ -121,13 +121,15 @@ Because the plugin has not yet been approved for the WordPress.org Plugin Direct
1. Sign in to WordPress Admin.
2. Open the HTBD settings page.
3. Enter the **APP ID** and **secret key** provided by the Baidu Translate Open Platform.
4. Set the website's source language.
4. Set the website's source language, or enable **Automatically detect the source language** to use Baidu's `auto` source language mode.
5. Add the target languages you want to enable.
6. Select a language URL mode, such as language subdirectories or domain binding.
7. Save the settings.
After changing language routing settings, open **Settings > Permalinks** in WordPress Admin and confirm that the permalink configuration is working correctly.
Automatic detection is stored as a separate setting. Entering `auto` in the source language field without enabling automatic detection remains a distinct configuration state.
### 4. Start Translating
1. Open the post or page you want to translate in WordPress Admin.
+3 -1
View File
@@ -121,13 +121,15 @@ HTBD 为多语言页面提供必要的 SEO 支持,包括:
1. 登录 WordPress 管理后台。
2. 打开 HTBD 设置页面。
3. 填写百度翻译开放平台提供的 **APP ID****密钥**
4. 设置网站的源语言。
4. 设置网站的源语言,或勾选“自动识别源语言”以使用百度翻译的 `auto` 源语言模式
5. 添加需要启用的目标语言。
6. 选择语言 URL 模式,例如语言子目录或域名绑定。
7. 保存设置。
修改语言路由设置后,建议在 WordPress 后台打开“设置” → “固定链接”,确认固定链接配置已经正确生效。
自动识别状态使用独立选项保存。未勾选自动识别时在源语言输入框中主动填写 `auto`,仍属于不同的配置状态。
### 四、开始使用
1. 在 WordPress 后台打开需要翻译的文章或页面。
+8
View File
@@ -3,6 +3,8 @@
var domainMode = document.querySelector('#htbd-routing-mode input[value="domain"]');
var bindingsRow = document.getElementById('htbd-domain-bindings-row');
var autoDetectSourceLanguage = document.getElementById('htbd-auto-detect-source-language');
var sourceLanguageRow = document.getElementById('htbd-source-language-row');
if (domainMode && bindingsRow) {
domainMode.addEventListener('change', function () {
@@ -10,6 +12,12 @@
});
}
if (autoDetectSourceLanguage && sourceLanguageRow) {
autoDetectSourceLanguage.addEventListener('change', function () {
sourceLanguageRow.style.display = autoDetectSourceLanguage.checked ? 'none' : '';
});
}
document.querySelectorAll('form[data-htbd-confirm]').forEach(function (form) {
form.addEventListener('submit', function (event) {
if (!window.confirm(form.getAttribute('data-htbd-confirm'))) {
+5 -2
View File
@@ -61,6 +61,7 @@ class HTBD_Admin {
public function sanitize_settings( $settings ) {
$settings = (array) $settings;
$bindings = array();
$auto_detect_source_language = ! empty( $settings['auto_detect_source_language'] );
$routing_modes = HTBD_Settings::routing_modes( $settings['routing_mode'] ?? array() );
$target_languages = array_values( array_filter( array_map( 'sanitize_key', explode( ',', (string) ( $settings['target_languages'] ?? 'en' ) ) ) ) );
@@ -74,7 +75,8 @@ class HTBD_Admin {
}
return array(
'source_language' => sanitize_key( $settings['source_language'] ?? 'zh' ),
'auto_detect_source_language' => $auto_detect_source_language,
'source_language' => $auto_detect_source_language ? 'auto' : sanitize_key( $settings['source_language'] ?? 'zh' ),
'target_languages' => $target_languages,
'routing_mode' => $routing_modes,
'domain_bindings' => $bindings,
@@ -97,7 +99,8 @@ class HTBD_Admin {
<form action="options.php" method="post">
<?php settings_fields( 'htbd_settings' ); ?>
<table class="form-table" role="presentation">
<tr><th scope="row"><label for="htbd-source-language"><?php esc_html_e( 'Source language', 'hyx-translator-for-baidu-translate' ); ?></label></th><td><input id="htbd-source-language" name="htbd_settings[source_language]" type="text" value="<?php echo esc_attr( $settings['source_language'] ); ?>" /><p class="description"><?php esc_html_e( 'Use a Baidu Translate language code, for example zh.', 'hyx-translator-for-baidu-translate' ); ?></p></td></tr>
<tr><th scope="row"><?php esc_html_e( 'Source language detection', 'hyx-translator-for-baidu-translate' ); ?></th><td><label for="htbd-auto-detect-source-language"><input id="htbd-auto-detect-source-language" name="htbd_settings[auto_detect_source_language]" type="checkbox" value="1" <?php checked( $settings['auto_detect_source_language'] ); ?> /> <?php esc_html_e( 'Automatically detect the source language', 'hyx-translator-for-baidu-translate' ); ?></label></td></tr>
<tr id="htbd-source-language-row"<?php echo $settings['auto_detect_source_language'] ? ' style="display:none"' : ''; ?>><th scope="row"><label for="htbd-source-language"><?php esc_html_e( 'Source language', 'hyx-translator-for-baidu-translate' ); ?></label></th><td><input id="htbd-source-language" name="htbd_settings[source_language]" type="text" value="<?php echo esc_attr( $settings['source_language'] ); ?>" /><p class="description"><?php esc_html_e( 'Use a Baidu Translate language code, for example zh.', 'hyx-translator-for-baidu-translate' ); ?></p></td></tr>
<tr><th scope="row"><label for="htbd-target-languages"><?php esc_html_e( 'Target languages', 'hyx-translator-for-baidu-translate' ); ?></label></th><td><input id="htbd-target-languages" name="htbd_settings[target_languages]" type="text" value="<?php echo esc_attr( implode( ',', $settings['target_languages'] ) ); ?>" class="regular-text" /><p class="description"><?php esc_html_e( 'Separate multiple language codes with commas, for example en,ja.', 'hyx-translator-for-baidu-translate' ); ?></p></td></tr>
<tr><th scope="row"><?php esc_html_e( 'URL mode', 'hyx-translator-for-baidu-translate' ); ?></th><td><fieldset id="htbd-routing-mode"><label><input type="checkbox" name="htbd_settings[routing_mode][]" value="subdirectory" <?php checked( in_array( 'subdirectory', $settings['routing_mode'], true ) ); ?> /> <?php esc_html_e( 'Subdirectory', 'hyx-translator-for-baidu-translate' ); ?></label><br /><label><input type="checkbox" name="htbd_settings[routing_mode][]" value="domain" <?php checked( in_array( 'domain', $settings['routing_mode'], true ) ); ?> /> <?php esc_html_e( 'Domain', 'hyx-translator-for-baidu-translate' ); ?></label></fieldset></td></tr>
<tr id="htbd-domain-bindings-row"<?php echo in_array( 'domain', $settings['routing_mode'], true ) ? '' : ' style="display:none"'; ?>><th scope="row"><label for="htbd-domain-bindings"><?php esc_html_e( 'Domain bindings', 'hyx-translator-for-baidu-translate' ); ?></label></th><td><textarea id="htbd-domain-bindings" name="htbd_settings[domain_bindings]" rows="4" class="large-text" placeholder="en.example.com=en"><?php echo esc_textarea( implode( "\n", $bindings ) ); ?></textarea><p class="description"><?php esc_html_e( 'Enter one domain=language code pair per line, for example en.example.com=en. Do not include https://, a path, or a port.', 'hyx-translator-for-baidu-translate' ); ?></p></td></tr>
+1
View File
@@ -40,6 +40,7 @@ class HTBD_Plugin {
add_option(
'htbd_settings',
array(
'auto_detect_source_language' => false,
'source_language' => 'zh',
'target_languages' => array( 'en' ),
'routing_mode' => array( 'subdirectory' ),
+1
View File
@@ -5,6 +5,7 @@ defined( 'ABSPATH' ) || exit;
class HTBD_Settings {
public static function get() {
$defaults = array(
'auto_detect_source_language' => false,
'source_language' => 'zh',
'target_languages' => array( 'en' ),
'routing_mode' => array( 'subdirectory' ),
@@ -32,6 +32,12 @@ msgstr "暂时无法读取翻译进度。"
msgid "Failed translations"
msgstr "翻译失败的内容"
msgid "Source language detection"
msgstr "源语言识别"
msgid "Automatically detect the source language"
msgstr "自动识别源语言"
msgid "Source language"
msgstr "源语言"
@@ -40,6 +40,12 @@ msgstr ""
msgid "Failed translations"
msgstr ""
msgid "Source language detection"
msgstr ""
msgid "Automatically detect the source language"
msgstr ""
#: includes/class-admin.php:95
msgid "Source language"
msgstr ""
+3 -1
View File
@@ -24,10 +24,12 @@ WordPress 后台界面会跟随当前 WordPress 语言:默认显示英文,Wo
1. 将 `hyx-translator-for-baidu-translate` 目录上传到 `/wp-content/plugins/`。
2. 在 WordPress 的“插件”页面启用 HTBD。
3. 打开“设置 > HTBD”,配置百度翻译凭据、源语言、目标语言和路由模式。
3. 打开“设置 > HTBD”,配置百度翻译凭据、源语言、目标语言和路由模式。勾选“自动识别源语言”可使用百度翻译的 `auto` 源语言模式。
4. 第一次保存设置后,手动执行“重新翻译所有内容”,为已有内容安排翻译任务。
5. 此后保存文章或页面时,插件会自动安排翻译任务。
自动识别状态使用独立选项保存。未勾选自动识别时主动将源语言填写为 `auto`,仍属于不同的配置状态。
== External Services ==
HTBD 连接由百度提供的百度翻译开放平台 API,以翻译 WordPress 内容。没有该外部服务,插件无法生成新的翻译。站点管理员需要百度翻译账户、应用 ID 和密钥;百度可能根据管理员选择的服务方案实施请求配额或收取使用费用。
+3 -1
View File
@@ -24,9 +24,11 @@ Subdirectory routing supports paths such as `/en/example-post/`. Domain routing
1. Upload the `hyx-translator-for-baidu-translate` directory to `/wp-content/plugins/`.
2. Activate HTBD through the Plugins screen in WordPress.
3. Open Settings > HTBD and configure the Baidu credentials, languages, and routing mode.
3. Open Settings > HTBD and configure the Baidu credentials, languages, and routing mode. Enable automatic source-language detection to use Baidu's `auto` source language mode.
4. After saving settings for the first time, manually run Retranslate all content to queue existing content.
Automatic detection is stored independently from the source language value. Manually entering `auto` without enabling automatic detection remains a distinct configuration state.
== External Services ==
HTBD connects to the Baidu Translate Open Platform API, a service provided by Baidu, to translate WordPress content. The plugin cannot generate new translations without this service. A Baidu Translate account, application ID, and secret key are required. Baidu may apply request quotas or usage charges according to the service plan selected by the site administrator.