i18n à 的开源项目脸书

发布于 2024-09-24 16:22:16 字数 655 浏览 3 评论 0原文

Facebook 对其网站本地化采用了这种独特而巧妙的方法:翻译人员(在他们的情况下是自愿帮助翻译网站的用户)只需单击其网站中尚未翻译的字符串(标有绿色底部边框)即可。网站上的自然环境。请参阅 http://www.facebook.com/translations/

现在,如果您曾经需要处理网站的翻译,您就会清楚地意识到,当使用 poedit 等工具时,其中一些翻译是多么奇怪和有趣,因为译者并不完全意识到翻译的位置字符串将出现在网站上。

示例:请翻译“Home”。例如,在德语中,网站的起始页是“Home”,而您居住的房子是“Heim”。现在,作为翻译者,您基本上必须猜测该术语可能出现在网站上的哪个上下文中,并进行相应的翻译。很有可能,您的新家居家具网站现在被翻译为“Home-Einrichtung”,这对任何德国人来说听起来都很荒谬。

所以,我的问题归结为:

您知道有任何开源 PHP 项目可以处理类似的事情吗?我基本上是在寻找一个框架,允许您将国际化网站置于“翻译模式”,并使字符串可点击和可翻译,例如通过 Javascript 模式。

我并不是在寻找一个成熟且 可翻译的框架。现成的解决方案,但很想了解我可以为其贡献代码的类似项目。

提前致谢!

Facebook has this unique and clever approach to localization of their site: translators (in their case users that help to translate the site voluntarily) can simply click on the not-yet-translated strings – which are marked with a green bottom border – in their natural context on the site. See http://www.facebook.com/translations/.

Now, if you ever had to deal with the translation of a website, you'll be well aware of how odd and funny some of these translations can be when using tools like poedit where the translator isn't fully aware of the spot the translated string will lated appear in on the website.

Example: Please translate "Home". In German, for instance, the start page of a website would be "Home" while the house you live in is "Heim". Now, you as the translator basically have to guess which context this term is likely to appear in on the website and translate accordingly. Chances are, you're new website on home furniture now translates as "Home-Einrichtung" which sounds ridiculous to any German.

So, my question boils down to:

Do you know any open source PHP projects that work on something like this? I'm basically looking for a framework that allows you to put your internationalized website in "translation mode" and make strings clickable and translatable e.g. through a Javascript modal.

I'm not so much looking for a full-fledged and ready-made solution, but would love to know about similar projects that I can contribute code to.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海 2024-10-01 16:22:16

如果你想用 jquery & 推出自己的jquery browserLanguage,这可能会让你继续。

使用 class="i18n" 标记所有可翻译文本的包含元素,并包括 jquery、jquery browserLanguage 和您的 i18n 脚本。

1国际化 javascript

——这需要通过 ajax 从您的服务器接受翻译,例如:

var i18n = {};
i18n.bank = new Array();
i18n.t = function ( text, tl=$.browserLanguage ) {
    var r = false;
    $.ajax({ 
        url: "/i18n_t.php?type=request&from="+ escape(text) +"&tl="+ tl, 
        success: function(){ i18n.bank[text] = this; r = true; }
    });
    return r;
};

2php i18n 翻译服务

—现在我们需要提供翻译并接受它们

数据库将看起来像一堆表格,每种语言一个。

// SCHEMA for each language:
CREATE TABLE `en`
(
`id` INT PRIMARY KEY AUTO INCREMENT NOT NULL,
`from` VARCHAR(500) NOT NULL,
`to` VARCHAR(500) NOT NULL
)

php 将需要一些连接和数据库操作.. 现在可以这样做:

//Connect to the database
$connection = mysql_connect('host (usually localhost)', 'mysql_username' , 'mysql_password');
$selection = mysql_select_db('mysql_database', $connection);

function table_exists($tablename, $database = false) {
    if(!$database) {
        $res = mysql_query("SELECT DATABASE()");
        $database = mysql_result($res, 0);
    }

    $res = mysql_query("SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = '$database' AND table_name = '$tablename'
    ");

    return mysql_result($res, 0) == 1;
}

代码很简单:

<?php     
// .. database stuff from above goes here ..
$type=$_GET["type"];
$from=$_GET["from"];
$to=$_GET["to"];
$tl=$_GET["tl"];

if (! table_exists($tl)) {
...
}

if ($type == "request") { // might want to set $tl="en" when ! table_exists($tl)
    $find = mysql_query("SELECT to FROM `'$tl'` WHERE from='$from'");
    $row = mysql_fetch_array($find);
    echo $row['to'];
} elsif ($type == "suggest") {
    $find = mysql_query("SELECT COUNT(*) AS count FROM `'$tl'` WHERE from='$from'");
    if ( !(mysql_result($res, 0)) == 0 ) { 
        $ins = mysql_query("INSERT INTO `'$tl'` (from, to) VALUES ('$from','$to')");
    }
}
?>

3页面翻译机制

——最后我们可以使用一些进一步的 jquery 将它们在您的网页中结合在一起:

i18n.suggest = function (from) { // post user translation to our php
    $.ajax({ 
        url: "/i18n_t.php?type=suggest&from='+from+'&to="+ escape( $('#i18n_s').contents() ) +"&tl="+ $.browserLanguage, 
        success: function(){ $('#i18n_t_div').html('<em>Thanks!</em>').delay(334).fadeOut().remove(); }
    });
};

$(document).ready(function() {
    i18n.t("submit");
    i18n.t("Thanks!");
    $('.i18n').click( function(event) { //add an onClick event for all i18n spans
        $('#i18n_t_div').remove;
        $(this).parent().append(
'<div id="i18n_t_div"><form class="i18n_t_form">
    <input type="text" id="i18n_s" name="suggestion" value="+$(this).contents()+" />
    <input type="button" value="'+ i18n.bank[ "submit" ] +'" onclick="i18n.suggest( '+$(this).contents()+' )" />
</form></div>'
        );
    }).each(function(){ 
        var c = $(this).contents(); //now load initial translations for browser language for all the internationalized content on the page
        if ( i18n.t(c) ){
            $(this).html(i18n.bank[c]);
        }
    });
}); 

请注意,我没有服务器来测试这个...而且我实际上并不编写 php 代码。 :D 这需要一些调试,但脚手架应该是正确的。

If you want to roll your own with jquery & jquery browserLanguage, this might get you going.

Tag all translatable text's contain elements with class="i18n", and include jquery, jquery browserLanguage, and your i18n script.

1. the internationalization javascript

— this needs to accept translations via ajax from your server, like:

var i18n = {};
i18n.bank = new Array();
i18n.t = function ( text, tl=$.browserLanguage ) {
    var r = false;
    $.ajax({ 
        url: "/i18n_t.php?type=request&from="+ escape(text) +"&tl="+ tl, 
        success: function(){ i18n.bank[text] = this; r = true; }
    });
    return r;
};

2. php i18n translation service

— now we need to serve up translations, and accept them

the database will look like a bunch of tables, one for each language.

// SCHEMA for each language:
CREATE TABLE `en`
(
`id` INT PRIMARY KEY AUTO INCREMENT NOT NULL,
`from` VARCHAR(500) NOT NULL,
`to` VARCHAR(500) NOT NULL
)

the php will need some connection and db manipulation.. for now this may do:

//Connect to the database
$connection = mysql_connect('host (usually localhost)', 'mysql_username' , 'mysql_password');
$selection = mysql_select_db('mysql_database', $connection);

function table_exists($tablename, $database = false) {
    if(!$database) {
        $res = mysql_query("SELECT DATABASE()");
        $database = mysql_result($res, 0);
    }

    $res = mysql_query("SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = '$database' AND table_name = '$tablename'
    ");

    return mysql_result($res, 0) == 1;
}

the code is simply:

<?php     
// .. database stuff from above goes here ..
$type=$_GET["type"];
$from=$_GET["from"];
$to=$_GET["to"];
$tl=$_GET["tl"];

if (! table_exists($tl)) {
...
}

if ($type == "request") { // might want to set $tl="en" when ! table_exists($tl)
    $find = mysql_query("SELECT to FROM `'$tl'` WHERE from='$from'");
    $row = mysql_fetch_array($find);
    echo $row['to'];
} elsif ($type == "suggest") {
    $find = mysql_query("SELECT COUNT(*) AS count FROM `'$tl'` WHERE from='$from'");
    if ( !(mysql_result($res, 0)) == 0 ) { 
        $ins = mysql_query("INSERT INTO `'$tl'` (from, to) VALUES ('$from','$to')");
    }
}
?>

3. page translation mechanics

— finally we can tie them together in your webpages with some further jquery:

i18n.suggest = function (from) { // post user translation to our php
    $.ajax({ 
        url: "/i18n_t.php?type=suggest&from='+from+'&to="+ escape( $('#i18n_s').contents() ) +"&tl="+ $.browserLanguage, 
        success: function(){ $('#i18n_t_div').html('<em>Thanks!</em>').delay(334).fadeOut().remove(); }
    });
};

$(document).ready(function() {
    i18n.t("submit");
    i18n.t("Thanks!");
    $('.i18n').click( function(event) { //add an onClick event for all i18n spans
        $('#i18n_t_div').remove;
        $(this).parent().append(
'<div id="i18n_t_div"><form class="i18n_t_form">
    <input type="text" id="i18n_s" name="suggestion" value="+$(this).contents()+" />
    <input type="button" value="'+ i18n.bank[ "submit" ] +'" onclick="i18n.suggest( '+$(this).contents()+' )" />
</form></div>'
        );
    }).each(function(){ 
        var c = $(this).contents(); //now load initial translations for browser language for all the internationalized content on the page
        if ( i18n.t(c) ){
            $(this).html(i18n.bank[c]);
        }
    });
}); 

Mind you I don't have a server to test this on... and I don't actually code php. :D It will take some debugging but the scaffolding should be correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文