data URI)。 * * 单独一份文件、按需加载:几十 KB 的 base64 没必要跟着每个请求走, * 只有前台真要显示图标时才读。 * * @return array */ public static function flags() { if ( null === self::$flags ) { $file = APGT_PLUGIN_DIR . 'includes/flags.php'; self::$flags = is_readable( $file ) ? (array) require $file : array(); } return self::$flags; } /** * 取某个语言的旗帜图标。 * * @param string $iso 语言代码。 * @return string data URI;没有就返回空串。 */ public static function flag( $iso ) { $flags = self::flags(); return isset( $flags[ $iso ] ) ? (string) $flags[ $iso ] : ''; } /** * 读取语言元数据静态表(iso => 信息)。 * * @return array */ public static function language_meta() { if ( null === self::$languages ) { self::$languages = require APGT_PLUGIN_DIR . 'includes/languages.php'; } return self::$languages; } /** * 语言与基本配置默认值。 * * @return array */ public static function default_settings() { return array( 'default_language' => '', // 站点源语言 iso,如 zh-Hans 'is_trans_url' => 0, // 是否连站内链接的路径一起翻译 'languages' => array(), // iso => 目标语言配置,见 default_language_row() 'auto_worker' => 0, // 管理员登录后台时自动翻译待办页面 'worker_interval' => 60, // worker 心跳间隔(秒) 'realtime_trans' => 0, // 访客访问译文页面时实时补翻 'realtime_ratio' => 100, // 译文占比达到多少 % 才输出译文页面 'fetch_ip' => '', // 抓取原页面时把站点域名固定解析到的 IP,留空走系统 DNS 'menu_container' => '', // 语言菜单挂载到哪个元素 id,留空则浮动显示 'show_switcher' => 1, // 是否输出语言切换菜单 'show_hreflang' => 1, // 是否输出 hreflang 'show_flag' => 1, // 语言菜单里是否显示旗帜小图标 ); } /** * 单个目标语言配置行的默认结构。 * * @param string $iso 语言代码。 * @return array */ public static function default_language_row( $iso = '' ) { $meta = self::language_meta(); return array( 'iso' => $iso, 'dir' => $iso, // URL 路径前缀 /en/ 'hreflang_name' => isset( $meta[ $iso ]['hreflang_name'] ) ? $meta[ $iso ]['hreflang_name'] : $iso, 'hreflang_area' => isset( $meta[ $iso ]['hreflang_area'] ) ? $meta[ $iso ]['hreflang_area'] : '', 'sort' => 0, 'mode' => '0', // 0所有用户 1仅搜索引擎 2仅普通用户 'text_replace' => array(), // [ {search, replace}, ... ] ); } /** * AI 服务配置默认值。 * * @return array */ public static function default_ai() { return array( // 由谁去请求 AI:'' 自动(站点级 AI 配置可用就用它,否则用下面这些字段)、 // 'core' 只用 WordPress 自带的 AI Client、'plugin' 只用下面这些字段、 // 'service' 用订阅服务(填一个访问密钥即可,不必自备 AI 平台账号)。 'provider' => '', // ── 订阅服务(Aipogeo)───────────────────────────────────── // 密钥在服务站点的会员中心「我的订阅」生成,只显示一次; // 插件只存这一个值,不保存任何 AI 平台的凭据。 'service_url' => 'https://translate.aipogeo.com', 'license_key' => '', 'license_site' => '', // 激活时登记的站点地址(服务端归一化后的值) 'license_checked' => 0, // 上次校验成功的时间戳 'license_snapshot' => array(), // 最近一次的订阅信息(额度/到期日),仅用于设置页展示 'platform' => '', // openai / claude / gemini / deepseek(由 model 自动解析,此处仅记录) 'model' => 'deepseek-chat', 'protocol' => '', // 协议格式,留空表示由模型名自动识别 'api_url' => '', // 接口地址,留空表示用 php-ai 库里该模型(或所选协议)的内置地址 'api_key' => '', 'proxy' => '', // socks5://host:port 或 http://host:port // 单次输出上限,不是上下文长度。各模型的上限差得很远(Claude 3 Opus 4K、 // GPT-4o 16K、DeepSeek 384K),填死一个值换模型就会被平台拒绝, // 所以默认 0=不传该参数,跟随所选模型在库里的内置值。 'max_tokens' => 0, 'timeout' => 300, ); } /** * 默认 AI 翻译指令。 * * 取自 website-dev「多语言翻译」默认指令(prompt_ask + prompt_output 合并), * 保留三个固定变量 {{language_from}} {{language_to}} {{translate_data}}。 * * @return string */ public static function default_prompt() { return <<<'PROMPT' You are a professional multilingual translator for SEO websites. Requirements: 1. The translation must read naturally, the way a native speaker of the target language would put it 2. Keep the original meaning; do not translate word for word 3. Polish the wording where that helps, so it works well for search engines (SEO friendly) 4. Keep the JSON structure unchanged. The JSON keys are the numbers I use to identify each source string — do not modify them 5. Return JSON only Notes: + A `%s` inside a string is a placeholder for the program's string formatting + Some source strings hold only part of a sentence, because the HTML text node they came from ends there. Do not expand them on your own, or the meaning will break once I put them back into the HTML + If a source string is a URL or a URI, keep its URI path information when translating Source language: {{language_from}} Target language: {{language_to}} Input: ```json {{translate_data}} ``` When returning the data, do not change the keys of the original data; output only the translated content for each original key. Output format: { "source number":"translated content" } PROMPT; } /** * 默认 URL 路径翻译指令。 * * 网址路径和正文的要求完全不同(要保持斜杠层级、不能带空格、扩展名与查询串 * 原样留着),混在正文指令里说不清楚,所以单独一条:开启「翻译 URL 路径」时, * 路径条目自成一批用这条指令送翻,正文指令不必写 URL 相关的要求。 * * 变量与正文指令一致:{{language_from}} {{language_to}} {{translate_data}}。 * * @return string */ public static function default_url_prompt() { return <<<'PROMPT' You are a professional multilingual translator for SEO websites. What you are translating here are the site's URL paths. Requirements: 1. Every word in the path that carries meaning must be translated into the target language. It makes no difference whether that segment was written in English, in pinyin or in some other language — translate it. Do not copy a segment back unchanged just because it looks like a common word (contact, about, news and the like) 2. Write it in the target language's own script; do not transliterate. If the target language is Japanese, write Japanese (kanji/kana); if it is Russian, write Cyrillic; if it is English, write English words — `/关于我们/` becomes `/about-us/` in English and `/私たちについて/` in Japanese, never a romanization such as `/watashitachi-nitsuite/` 3. Follow the writing conventions of the target language: languages written in the Latin alphabet (English, French, Vietnamese and so on) go all lowercase with words joined by a hyphen `-`; languages not written in the Latin alphabet (Japanese, Korean, Chinese, Russian and so on) are written the way that language normally writes, with no hyphens between words. No language may contain spaces 4. Keep the path structure unchanged: the leading `/` must stay, a trailing `/` must be present or absent exactly as in the original, and the number of segments must not change 5. Only these are kept exactly as they are: plain numbers, dates, version numbers, random strings and identifiers, all-caps abbreviations and product model numbers (WMS, SEO, API, X100 and so on), brand names, file extensions (.html/.php and so on), query strings (`?a=b`) and anchors (`#name`) 6. Check every entry and leave none untranslated: apply the rules above to each one before returning it, and do not copy an entry back unchanged just because it looks familiar 7. Keep the JSON structure unchanged. The JSON keys are the numbers I use to identify each source string — do not modify them 8. Return JSON only Notes: + What you return must be a path: no domain, no `http://` and no `https://` + The path is read by search engines, so use the wording that is most common in the target language rather than a word-for-word rendering + If no perfect wording comes to mind, still express the meaning in the target language; never fall back to copying the original as the translation Source language: {{language_from}} Target language: {{language_to}} Input: ```json {{translate_data}} ``` When returning the data, do not change the keys of the original data; output only the translated path for each original key. Output format: { "source number":"/translated/path/" } PROMPT; } /** * 读取语言与基本配置。 * * @return array */ public static function get_settings() { $saved = get_option( self::OPT_SETTINGS ); if ( ! is_array( $saved ) ) { $saved = array(); } return array_merge( self::default_settings(), $saved ); } /** * 保存语言与基本配置。 * * @param array $settings 配置数组。 * @return void */ public static function save_settings( array $settings ) { update_option( self::OPT_SETTINGS, $settings ); } /** * 读取 AI 服务配置。 * * @return array */ public static function get_ai() { $saved = get_option( self::OPT_AI ); if ( ! is_array( $saved ) ) { $saved = array(); } return array_merge( self::default_ai(), $saved ); } /** * 保存 AI 服务配置。 * * @param array $ai AI 配置数组。 * @return void */ public static function save_ai( array $ai ) { update_option( self::OPT_AI, $ai ); } /** * 读取 AI 翻译指令(未设置时返回默认)。 * * @return string */ public static function get_prompt() { $saved = get_option( self::OPT_PROMPT ); if ( ! is_string( $saved ) || '' === trim( $saved ) ) { return self::default_prompt(); } return $saved; } /** * 保存 AI 翻译指令。 * * @param string $prompt 指令文本。 * @return void */ public static function save_prompt( $prompt ) { update_option( self::OPT_PROMPT, (string) $prompt ); } /** * 读取 URL 路径翻译指令(未设置时返回默认)。 * * @return string */ public static function get_url_prompt() { $saved = get_option( self::OPT_URL_PROMPT ); if ( ! is_string( $saved ) || '' === trim( $saved ) ) { return self::default_url_prompt(); } return $saved; } /** * 保存 URL 路径翻译指令。 * * @param string $prompt 指令文本。 * @return void */ public static function save_url_prompt( $prompt ) { update_option( self::OPT_URL_PROMPT, (string) $prompt ); } /** * 取站点源语言 iso。 * * @return string */ public static function default_language() { $s = self::get_settings(); return (string) $s['default_language']; } /** * 取已配置的目标语言(不含源语言),按 sort 升序。 * * @return array iso => 语言配置行 */ public static function target_languages() { $s = self::get_settings(); $default = $s['default_language']; $langs = is_array( $s['languages'] ) ? $s['languages'] : array(); // 过滤掉与源语言相同的项。 $targets = array(); foreach ( $langs as $iso => $row ) { if ( '' === $iso || $iso === $default ) { continue; } $targets[ $iso ] = $row; } uasort( $targets, function ( $a, $b ) { return ( (int) $a['sort'] ) <=> ( (int) $b['sort'] ); } ); return $targets; } /** * 取语言目录(URL 前缀)到 iso 的映射。 * * @return array dir => iso */ public static function dir_to_iso_map() { $map = array(); foreach ( self::target_languages() as $iso => $row ) { $dir = isset( $row['dir'] ) && '' !== $row['dir'] ? $row['dir'] : $iso; $map[ $dir ] = $iso; } return $map; } }