找到你要的答案

Q:Moodle: How to add new independent global language file?

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

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

  • Where should I put this file?
  • How I should call my get_translate function?

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

  • Where should I put this file?
  • How I should call my get_translate function?
answer1: 回答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.

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:

/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.

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

对于本地插件的文件夹

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

然后文件应该包含

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

然后得到字符串使用

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

默认情况下,您需要有一个EN文件夹与英文字符串。

但您也可以使用ISO代码添加其他语言。例如添加波斯/波斯语代码发。因此创建一个附加文件夹:

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

然后在文件添加:

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

你也将需要安装的波斯语语言包。去网站管理员- >;语言包,或者干脆直接去找yourmoodlesite.com/admin/tool/langimport/index.php选择波斯语。

如果你想将现有的Moodle的字符串,那么它可能是更好的翻译,他们通过阿摩司。然后每个人都将受益于翻译。https://docs.moodle.org/dev/contributing_a_translation

answer2: 回答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.

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.

<?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.

$("#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
$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.

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.

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.

<?php

function get_translate($value){

$translations=array(

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


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

然后,您需要更改您需要的文件中的字符串:

<?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.

$("#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";
            }
});

最后打电话给你的语言包的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  localization  moodle  localizable.strings