簡體   English   中英

wordpress 插件翻譯不起作用

[英]wordpress plugin translation does not work

我創建了一個插件“translation_test”來測試 wordpress 的翻譯能力。

翻譯文件

在語言目錄中有一個文件 translation_test.pot 包含:

msgid "Testing"
msgstr ""

和一個文件 translation_test-de_DE.po:

msgid "Testing"
msgstr "Das ist ein test"

使用 gettext 我已經使用以下命令創建了相應的 .mo 文件:

msgfmt translation_test-de_DE.po -o translation_test-de_DE.mo

我已將所有 3 個文件上傳到插件的語言目錄:./translation_test/languages/

插件php文件:

還有一個名為“translation_test”的 php 文件,其中加載了文本域,並創建了一個管理頁面以顯示翻譯后的文本:

<?php

/*
Plugin Name: translation_test
Plugin URI: 
Description: translation_test
Version: 1.0
Author: 
Author URI: 
Text Domain: translation_test
*/

function translation_test_init_textdomain()
{
    load_plugin_textdomain( 'translation_test', false, dirname( plugin_basename( '__FILE__' ) ) . '/languages/' );
}
add_action( 'init', 'translation_test_init_textdomain' );

/**
 * Register a custom menu page.
 */
function wpdocs_register_my_custom_menu_page(){
    add_menu_page( 'title','custom menu','manage_options','custompage','my_custom_menu_page','',6 ); 
}
add_action( 'admin_menu', 'wpdocs_register_my_custom_menu_page' );
 
function my_custom_menu_page()
{
    echo __( 'Testing', 'translation_test' );
}

?>

語言設置

在 wordpress 的管理區域中,我將語言設置為德語,另外我在 wp-config.php 文件中添加了以下行:

define ('WPLANG', 'de_DE');

可悲的是翻譯不起作用,它總是顯示“測試”。 有誰知道錯誤在哪里? 是否有一些簡單的插件模板具有我可以使用的工作翻譯?

如果您沒有制作插件或主題,您只是將 wp-load.php 加載到您的應用程序中,請嘗試這樣做。 嘗試這個:

//get User Locale
$user_locale = get_user_locale();

// load text domain and .mo file
load_textdomain('your_text_domain', $_SERVER['DOCUMENT_ROOT'].'/wp-content/plugins/your_plugin/your_text_domain-'.$user_locale.'.mo');

// function to use with locale hook
function wpsx_redefine_locale($locale){
  global $user_locale;
  if ($user_locale == ''){
    return "en_US";
  } else {
    return $user_locale;
  }
}

// define locale hook
add_filter('locale','wpsx_redefine_locale',10);
// test translation
echo __('test','your_text_domain');

Obs:我使用 LOCO translate 來管理我的文本域以及 .mo 和 .po 文件。 https://br.wordpress.org/plugins/loco-translate/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM