1

我想翻译一个列出文件夹内容的脚本。包含文件、文件夹和显示的总大小的摘要。到目前为止,我使用 翻译了所有字符串gettext,但我被困在这里并寻找一种优雅的方法来解决翻译可能包含多个复数的字符串。可能不用说,但由于每种语言都不同,我想避免连接字符串。

$summary = sprintf(_('%1$s folders and %2$s files, %3$s %4$s in total'), $total_folders, $total_files, $total_size, $unit);

可能的状态(0-cases 被有意遗漏):

  • 1 个文件夹和 1 个文件,共 100 KB
  • 1 个文件夹和 2 个文件,共 200 KB
  • 2 个文件夹和 1 个文件,共 100 KB
  • 2 个文件夹和 2 个文件,共 200 KB

我认为ngettext()这将是 的适当替代品_(),但将其与我的示例结合起来让我无法理解。

4

2 回答 2

0

The pluralization is a basical problem with translations. Here is an example of implementations that can probably help you: http://symfony.com/doc/current/components/translation/usage.html#pluralization

Maybe you should consider to take the translation component they did for symfony.

于 2014-11-25T22:56:42.243 回答
0

我有解决方案。但是,也许你不知道 - 许多语言都有三种复数形式。

function plural($n, $f1, $f3, $f5)
{
    return $n%10==1&&$n%100!=11?$f1:($n%10>=2&&$n%10<=4&&($n%100<10||$n%100>=20)?$f3:$f5);
}
// for english
echo plural($x, 'monkey', 'monkeys', 'monkeys');
于 2014-11-25T23:01:30.830 回答