Moodle:如何添加新的独立全局语言文件?

时间:2023-02-05 12:59:57

I am customizing moodle in a new project. I want to add my limited strings to my special language file, in order to call it in any place. So I need to add a new file to language packs, e.g. bahar.php

我在一个新项目中定制moodle。我想将我的有限字符串添加到我的特殊语言文件中,以便在任何地方调用它。所以我需要在语言包中添加一个新文件,例如bahar.php

  • Where should I put this file?
  • 我应该把这个文件放在哪里?

  • How I should call my get_translate function?
  • 我应该如何调用我的get_translate函数?

2 个解决方案

#1


For a local plugin the folder is

对于本地插件,该文件夹是

/local/yourpluginname/lang/en/local_yourpluginname.php

Then the file should contain

然后该文件应包含

defined('MOODLE_INTERNAL') || die();
$string['pluginname'] = 'Your plugin name';
$string['mycustomstring'] = 'A custom string';

Then get the string using

然后使用获取字符串

echo get_string('mycustomstring', 'local_yourpluginname');

By default you will need to have an en folder with English strings.

默认情况下,您需要一个包含英文字符串的en文件夹。

But then you can also add additional languages using the iso code. For example to add Persian/Farsi the code is fa. So create an additional folder:

但是,您还可以使用iso代码添加其他语言。例如,要添加波斯语/波斯语,代码就是fa。所以创建一个额外的文件夹

/local/yourpluginname/lang/fa/local_yourpluginname.php

Then in the file add:

然后在文件中添加:

defined('MOODLE_INTERNAL') || die();
$string['pluginname'] = 'نام افزونه شما';
$string['mycustomstring'] = 'رشته های سفارشی';

You will also need to install the Farsi language pack. Go to site admin -> language packs or simply go direct to yourmoodlesite.com/admin/tool/langimport/index.php and select Farsi.

您还需要安装Farsi语言包。转到网站管理员 - >语言包或直接转到yourmoodlesite.com/admin/tool/langimport/index.php并选择波斯语。

If you want to translate the existing Moodle strings, then it might be better to translate them via AMOS. Then everyone will benefit from the translations. https://docs.moodle.org/dev/Contributing_a_translation

如果您想翻译现有的Moodle字符串,那么最好通过AMOS进行翻译。然后每个人都将从翻译中受益。 https://docs.moodle.org/dev/Contributing_a_translation

#2


I will explain how to do it for a generic PHP project, which should work for Moodle too. Firstly create a separate folder named lang in your project. This folder can be used to put your language packs and unicode handler libraries. For unicode handling , save the following file as portable-utf8.php in the lang folder.

我将解释如何为一个通用的PHP项目做这件事,它也适用于Moodle。首先在项目中创建一个名为lang的单独文件夹。此文件夹可用于放置语言包和unicode处理程序库。对于unicode处理,将以下文件保存为lang文件夹中的portable-utf8.php。

portable-utf8.php

Your language pack files can be named as bahar.php, tamil.php etc. and saved in the same location. A sample file should look like this; with the keyword string followed by the translation.

您的语言包文件可以命名为bahar.php,tamil.php等,并保存在同一位置。示例文件应如下所示;使用关键字字符串后跟翻译。

<?php

function get_translate($value){

$translations=array(

        "yourstring1"=>"விஷேட அம்சங்கள்",
        "yourstring2"=>"பின் செல்ல",
        "yourstring3"=>"தொலைபேசி எண்",
        "yourstring4"=>"தேர்ந்தெடுக்கப்பட்ட பண்ணை",
        "yourstring5"=>"கிராமம்",
            );


 return utf8_html_encode($translations[$value]);
}                   
?>

Then you need to change the strings in the files you need with this:

然后你需要改变你需要的文件中的字符串:

<?php echo get_translate("your_string");?>

Please note that this string should match with the string in your language pack file. The language of your choice is passed from page to page through the GET method. So you will need to pass it when when linking. Optionally, if you want to do dynamic translation on a button click, you can just use this jQuery code.

请注意,此字符串应与语言包文件中的字符串匹配。您选择的语言通过GET方法从一个页面传递到另一个页面。所以你需要在链接时传递它。 (可选)如果要在单击按钮上进行动态转换,则可以使用此jQuery代码。

$("#yourid").on('click', function(e) {
          var split = location.search.replace('?', '').split('=');
        if(split[1]=='yourlang1'){
            window.location.href="yourdir/yourfile1.php?lang=yourlang1";
            }
        else if (split[1]=='yourlang2'){
            window.location.href="yourdir/yourfile2.php?lang=yourlang2";
            }
        else if (split[1]=='yourlang'){
            window.location.href=""yourdir/yourfile3.php?lang=yourlang3";
            }
        else{
            window.location.href="yourdir/yourfile4.php";
            }
});

Finally call your language pack on the top of the PHP file where the translation needs to be done; like this:

最后在PHP文件的顶部调用您的语言包,在那里需要进行翻译;像这样:

<?php
$lng='';
// setting up language
require 'yourdir/lang/portable-utf8.php';
if(isset($_GET['lang'])){
$lng=$_GET['lang'];
      if($_GET['lang']=="yourlang1"){

        require 'yourdir/lang/yourlang1.php';
      }
      elseif ($_GET['lang']=="yourlang2")
      {
          require 'yourdir/lang/yourlang2.php';
      }
      else{
        require 'yourdir/lang/yourlang3.php';
    }
    }else{
      $lng='yourlangdefault';
require 'lang/yourlangdefualt.php';
    }

?>

this might look lengthy at first, but when you have hundreds, or maybe thousands of strings to add, this is the easiest way. All you need to do is add the PHP code to the top of your page, replace the string as given above with a single line of code and add the string to your language pack file if it does not exist. If you need any further explaination, please let me know.

这可能看起来很长,但是当你有数百或者数千个字符串要添加时,这是最简单的方法。您需要做的就是将PHP代码添加到页面顶部,用一行代码替换上面给出的字符串,并将字符串添加到语言包文件中(如果它不存在)。如果您需要进一步解释,请告诉我。

#1


For a local plugin the folder is

对于本地插件,该文件夹是

/local/yourpluginname/lang/en/local_yourpluginname.php

Then the file should contain

然后该文件应包含

defined('MOODLE_INTERNAL') || die();
$string['pluginname'] = 'Your plugin name';
$string['mycustomstring'] = 'A custom string';

Then get the string using

然后使用获取字符串

echo get_string('mycustomstring', 'local_yourpluginname');

By default you will need to have an en folder with English strings.

默认情况下,您需要一个包含英文字符串的en文件夹。

But then you can also add additional languages using the iso code. For example to add Persian/Farsi the code is fa. So create an additional folder:

但是,您还可以使用iso代码添加其他语言。例如,要添加波斯语/波斯语,代码就是fa。所以创建一个额外的文件夹

/local/yourpluginname/lang/fa/local_yourpluginname.php

Then in the file add:

然后在文件中添加:

defined('MOODLE_INTERNAL') || die();
$string['pluginname'] = 'نام افزونه شما';
$string['mycustomstring'] = 'رشته های سفارشی';

You will also need to install the Farsi language pack. Go to site admin -> language packs or simply go direct to yourmoodlesite.com/admin/tool/langimport/index.php and select Farsi.

您还需要安装Farsi语言包。转到网站管理员 - >语言包或直接转到yourmoodlesite.com/admin/tool/langimport/index.php并选择波斯语。

If you want to translate the existing Moodle strings, then it might be better to translate them via AMOS. Then everyone will benefit from the translations. https://docs.moodle.org/dev/Contributing_a_translation

如果您想翻译现有的Moodle字符串,那么最好通过AMOS进行翻译。然后每个人都将从翻译中受益。 https://docs.moodle.org/dev/Contributing_a_translation

#2


I will explain how to do it for a generic PHP project, which should work for Moodle too. Firstly create a separate folder named lang in your project. This folder can be used to put your language packs and unicode handler libraries. For unicode handling , save the following file as portable-utf8.php in the lang folder.

我将解释如何为一个通用的PHP项目做这件事,它也适用于Moodle。首先在项目中创建一个名为lang的单独文件夹。此文件夹可用于放置语言包和unicode处理程序库。对于unicode处理,将以下文件保存为lang文件夹中的portable-utf8.php。

portable-utf8.php

Your language pack files can be named as bahar.php, tamil.php etc. and saved in the same location. A sample file should look like this; with the keyword string followed by the translation.

您的语言包文件可以命名为bahar.php,tamil.php等,并保存在同一位置。示例文件应如下所示;使用关键字字符串后跟翻译。

<?php

function get_translate($value){

$translations=array(

        "yourstring1"=>"விஷேட அம்சங்கள்",
        "yourstring2"=>"பின் செல்ல",
        "yourstring3"=>"தொலைபேசி எண்",
        "yourstring4"=>"தேர்ந்தெடுக்கப்பட்ட பண்ணை",
        "yourstring5"=>"கிராமம்",
            );


 return utf8_html_encode($translations[$value]);
}                   
?>

Then you need to change the strings in the files you need with this:

然后你需要改变你需要的文件中的字符串:

<?php echo get_translate("your_string");?>

Please note that this string should match with the string in your language pack file. The language of your choice is passed from page to page through the GET method. So you will need to pass it when when linking. Optionally, if you want to do dynamic translation on a button click, you can just use this jQuery code.

请注意,此字符串应与语言包文件中的字符串匹配。您选择的语言通过GET方法从一个页面传递到另一个页面。所以你需要在链接时传递它。 (可选)如果要在单击按钮上进行动态转换,则可以使用此jQuery代码。

$("#yourid").on('click', function(e) {
          var split = location.search.replace('?', '').split('=');
        if(split[1]=='yourlang1'){
            window.location.href="yourdir/yourfile1.php?lang=yourlang1";
            }
        else if (split[1]=='yourlang2'){
            window.location.href="yourdir/yourfile2.php?lang=yourlang2";
            }
        else if (split[1]=='yourlang'){
            window.location.href=""yourdir/yourfile3.php?lang=yourlang3";
            }
        else{
            window.location.href="yourdir/yourfile4.php";
            }
});

Finally call your language pack on the top of the PHP file where the translation needs to be done; like this:

最后在PHP文件的顶部调用您的语言包,在那里需要进行翻译;像这样:

<?php
$lng='';
// setting up language
require 'yourdir/lang/portable-utf8.php';
if(isset($_GET['lang'])){
$lng=$_GET['lang'];
      if($_GET['lang']=="yourlang1"){

        require 'yourdir/lang/yourlang1.php';
      }
      elseif ($_GET['lang']=="yourlang2")
      {
          require 'yourdir/lang/yourlang2.php';
      }
      else{
        require 'yourdir/lang/yourlang3.php';
    }
    }else{
      $lng='yourlangdefault';
require 'lang/yourlangdefualt.php';
    }

?>

this might look lengthy at first, but when you have hundreds, or maybe thousands of strings to add, this is the easiest way. All you need to do is add the PHP code to the top of your page, replace the string as given above with a single line of code and add the string to your language pack file if it does not exist. If you need any further explaination, please let me know.

这可能看起来很长,但是当你有数百或者数千个字符串要添加时,这是最简单的方法。您需要做的就是将PHP代码添加到页面顶部,用一行代码替换上面给出的字符串,并将字符串添加到语言包文件中(如果它不存在)。如果您需要进一步解释,请告诉我。