Generate plural forms in tinygettext using python script
This commit is contained in:
parent
1a8b5cd2e1
commit
f1fb30250b
@ -18,8 +18,8 @@
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
|
||||
#include "tinygettext/plural_forms.hpp"
|
||||
#include "plural_forms_generated.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace tinygettext {
|
||||
|
||||
@ -34,56 +34,21 @@ namespace {
|
||||
* msgstr[1] = "You got %d errors";
|
||||
* ^-- return value of plural function
|
||||
*/
|
||||
unsigned int plural1(int ) { return 0; }
|
||||
unsigned int plural2_1(int n) { return (n != 1); }
|
||||
unsigned int plural2_2(int n) { return (n > 1); }
|
||||
unsigned int plural2_mk(int n) { return n==1 || n%10==1 ? 0 : 1; }
|
||||
unsigned int plural3_lv(int n) { return static_cast<unsigned int>(n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2); }
|
||||
unsigned int plural3_ga(int n) { return static_cast<unsigned int>(n==1 ? 0 : n==2 ? 1 : 2); }
|
||||
unsigned int plural3_lt(int n) { return static_cast<unsigned int>(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2); }
|
||||
unsigned int plural3_1(int n) { return static_cast<unsigned int>(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); }
|
||||
unsigned int plural3_sk(int n) { return static_cast<unsigned int>( (n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2 ); }
|
||||
unsigned int plural3_pl(int n) { return static_cast<unsigned int>(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); }
|
||||
unsigned int plural3_sl(int n) { return static_cast<unsigned int>(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3); }
|
||||
unsigned int plural4_gd(int n) { return static_cast<unsigned int>( n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3; }
|
||||
unsigned int plural6_ar(int n) { return static_cast<unsigned int>( n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5); }
|
||||
unsigned int plural(int ) { return 0; }
|
||||
|
||||
} // namespace
|
||||
|
||||
PluralForms
|
||||
PluralForms::from_string(const std::string& str)
|
||||
{
|
||||
typedef std::unordered_map<std::string, PluralForms> PluralFormsMap;
|
||||
static PluralFormsMap plural_forms;
|
||||
|
||||
if (plural_forms.empty())
|
||||
{
|
||||
// Note that the plural forms here shouldn't contain any spaces
|
||||
plural_forms["Plural-Forms:nplurals=1;plural=0;"] = PluralForms(1, plural1);
|
||||
plural_forms["Plural-Forms:nplurals=2;plural=(n!=1);"] = PluralForms(2, plural2_1);
|
||||
plural_forms["Plural-Forms:nplurals=2;plural=n!=1;"] = PluralForms(2, plural2_1);
|
||||
plural_forms["Plural-Forms:nplurals=2;plural=(n>1);"] = PluralForms(2, plural2_2);
|
||||
plural_forms["Plural-Forms:nplurals=2;plural=n==1||n%10==1?0:1;"] = PluralForms(2, plural2_mk);
|
||||
plural_forms["Plural-Forms:nplurals=3;plural=n%10==1&&n%100!=11?0:n!=0?1:2);"] = PluralForms(2, plural3_lv);
|
||||
plural_forms["Plural-Forms:nplurals=3;plural=n==1?0:n==2?1:2;"] = PluralForms(3, plural3_ga);
|
||||
plural_forms["Plural-Forms:nplurals=3;plural=(n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?1:2);"] = PluralForms(3, plural3_lt);
|
||||
plural_forms["Plural-Forms:nplurals=3;plural=(n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2);"] = PluralForms(3, plural3_1);
|
||||
plural_forms["Plural-Forms:nplurals=3;plural=(n==1)?0:(n>=2&&n<=4)?1:2;"] = PluralForms(3, plural3_sk);
|
||||
plural_forms["Plural-Forms:nplurals=3;plural=(n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2);"] = PluralForms(3, plural3_pl);
|
||||
plural_forms["Plural-Forms:nplurals=3;plural=(n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3);"] = PluralForms(3, plural3_sl);
|
||||
|
||||
plural_forms["Plural-Forms:nplurals=4;plural=(n==1||n==11)?0:(n==2||n==12)?1:(n>2&&n<20)?2:3;"]=PluralForms(4, plural4_gd);
|
||||
plural_forms["Plural-Forms:nplurals=6;plural=n==0?0:n==1?1:n==2?2:n%100>=3&&n%100<=10?3:n%100>=11?4:5"]=PluralForms(6, plural6_ar);
|
||||
}
|
||||
|
||||
// Remove spaces from string before lookup
|
||||
std::string space_less_str;
|
||||
for(std::string::size_type i = 0; i < str.size(); ++i)
|
||||
if (!isspace(str[i]))
|
||||
space_less_str += str[i];
|
||||
|
||||
PluralFormsMap::const_iterator it= plural_forms.find(space_less_str);
|
||||
if (it != plural_forms.end())
|
||||
std::unordered_map<std::string, PluralForms>::const_iterator it= g_plural_forms.find(space_less_str);
|
||||
if (it != g_plural_forms.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
|
26
lib/tinygettext/src/plural_forms_generated.hpp
Normal file
26
lib/tinygettext/src/plural_forms_generated.hpp
Normal file
@ -0,0 +1,26 @@
|
||||
// Generated by update_plural_forms.py, do not edit
|
||||
#include <unordered_map>
|
||||
std::unordered_map<std::string, tinygettext::PluralForms> g_plural_forms = {
|
||||
{ "Plural-Forms:nplurals=4;plural=(n%1==0&&n%10==1&&n%100!=11?0:n%1==0&&n%10>=2&&n%10<=4&&(n%100<12||n%100>14)?1:n%1==0&&(n%10==0||(n%10>=5&&n%10<=9)||(n%100>=11&&n%100<=14))?2:3);", tinygettext::PluralForms(4, [](int n)-> unsigned int { return (n%1==0&&n%10==1&&n%100!=11?0:n%1==0&&n%10>=2&&n%10<=4&&(n%100<12||n%100>14)?1:n%1==0&&(n%10==0||(n%10>=5&&n%10<=9)||(n%100>=11&&n%100<=14))?2:3); }) }, // uk
|
||||
{ "Plural-Forms:nplurals=3;plural=n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2;", tinygettext::PluralForms(3, [](int n)-> unsigned int { return n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2; }) }, // hr
|
||||
{ "Plural-Forms:nplurals=6;plural=n==0?0:n==1?1:n==2?2:n%100>=3&&n%100<=10?3:n%100>=11&&n%100<=99?4:5;", tinygettext::PluralForms(6, [](int n)-> unsigned int { return n==0?0:n==1?1:n==2?2:n%100>=3&&n%100<=10?3:n%100>=11&&n%100<=99?4:5; }) }, // ar
|
||||
{ "Plural-Forms:nplurals=2;plural=(n!=1);", tinygettext::PluralForms(2, [](int n)-> unsigned int { return (n!=1); }) }, // eu
|
||||
{ "Plural-Forms:nplurals=4;plural=(n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<12||n%100>14)?1:n%10==0||(n%10>=5&&n%10<=9)||(n%100>=11&&n%100<=14)?2:3);", tinygettext::PluralForms(4, [](int n)-> unsigned int { return (n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<12||n%100>14)?1:n%10==0||(n%10>=5&&n%10<=9)||(n%100>=11&&n%100<=14)?2:3); }) }, // ru
|
||||
{ "Plural-Forms:nplurals=1;plural=0;", tinygettext::PluralForms(1, [](int n)-> unsigned int { return 0; }) }, // th
|
||||
{ "Plural-Forms:nplurals=3;plural=(n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2);", tinygettext::PluralForms(3, [](int n)-> unsigned int { return (n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2); }) }, // szl
|
||||
{ "Plural-Forms:nplurals=5;plural=((n%10==1)&&(n%100!=11)&&(n%100!=71)&&(n%100!=91)?0:(n%10==2)&&(n%100!=12)&&(n%100!=72)&&(n%100!=92)?1:(n%10==3||n%10==4||n%10==9)&&(n%100<10||n%100>19)&&(n%100<70||n%100>79)&&(n%100<90||n%100>99)?2:(n!=0&&n%1000000==0)?3:4);", tinygettext::PluralForms(5, [](int n)-> unsigned int { return ((n%10==1)&&(n%100!=11)&&(n%100!=71)&&(n%100!=91)?0:(n%10==2)&&(n%100!=12)&&(n%100!=72)&&(n%100!=92)?1:(n%10==3||n%10==4||n%10==9)&&(n%100<10||n%100>19)&&(n%100<70||n%100>79)&&(n%100<90||n%100>99)?2:(n!=0&&n%1000000==0)?3:4); }) }, // br
|
||||
{ "Plural-Forms:nplurals=3;plural=(n==1?0:n==2?1:2);", tinygettext::PluralForms(3, [](int n)-> unsigned int { return (n==1?0:n==2?1:2); }) }, // kw
|
||||
{ "Plural-Forms:nplurals=4;plural=(n==1&&n%1==0)?0:(n>=2&&n<=4&&n%1==0)?1:(n%1!=0)?2:3;", tinygettext::PluralForms(4, [](int n)-> unsigned int { return (n==1&&n%1==0)?0:(n>=2&&n<=4&&n%1==0)?1:(n%1!=0)?2:3; }) }, // cs
|
||||
{ "Plural-Forms:nplurals=4;plural=(n==1&&n%1==0)?0:(n==2&&n%1==0)?1:(n%10==0&&n%1==0&&n>10)?2:3;", tinygettext::PluralForms(4, [](int n)-> unsigned int { return (n==1&&n%1==0)?0:(n==2&&n%1==0)?1:(n%10==0&&n%1==0&&n>10)?2:3; }) }, // he
|
||||
{ "Plural-Forms:nplurals=2;plural=(n>1);", tinygettext::PluralForms(2, [](int n)-> unsigned int { return (n>1); }) }, // oc
|
||||
{ "Plural-Forms:nplurals=2;plural=(n%10!=1||n%100==11);", tinygettext::PluralForms(2, [](int n)-> unsigned int { return (n%10!=1||n%100==11); }) }, // is
|
||||
{ "Plural-Forms:nplurals=4;plural=(n==1||n==11)?0:(n==2||n==12)?1:(n>2&&n<20)?2:3;", tinygettext::PluralForms(4, [](int n)-> unsigned int { return (n==1||n==11)?0:(n==2||n==12)?1:(n>2&&n<20)?2:3; }) }, // gd
|
||||
{ "Plural-Forms:nplurals=4;plural=(n%10==1&&(n%100>19||n%100<11)?0:(n%10>=2&&n%10<=9)&&(n%100>19||n%100<11)?1:n%1!=0?2:3);", tinygettext::PluralForms(4, [](int n)-> unsigned int { return (n%10==1&&(n%100>19||n%100<11)?0:(n%10>=2&&n%10<=9)&&(n%100>19||n%100<11)?1:n%1!=0?2:3); }) }, // lt
|
||||
{ "Plural-Forms:nplurals=3;plural=(n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2);", tinygettext::PluralForms(3, [](int n)-> unsigned int { return (n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2); }) }, // bs
|
||||
{ "Plural-Forms:nplurals=4;plural=(n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3);", tinygettext::PluralForms(4, [](int n)-> unsigned int { return (n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3); }) }, // sl
|
||||
{ "Plural-Forms:nplurals=4;plural=(n%1==0&&n==1?0:n%1==0&&n>=2&&n<=4?1:n%1!=0?2:3);", tinygettext::PluralForms(4, [](int n)-> unsigned int { return (n%1==0&&n==1?0:n%1==0&&n>=2&&n<=4?1:n%1!=0?2:3); }) }, // sk
|
||||
{ "Plural-Forms:nplurals=3;plural=(n%10==1&&n%100!=11?0:n!=0?1:2);", tinygettext::PluralForms(3, [](int n)-> unsigned int { return (n%10==1&&n%100!=11?0:n!=0?1:2); }) }, // lv
|
||||
{ "Plural-Forms:nplurals=5;plural=(n==1?0:n==2?1:n<7?2:n<11?3:4);", tinygettext::PluralForms(5, [](int n)-> unsigned int { return (n==1?0:n==2?1:n<7?2:n<11?3:4); }) }, // ga
|
||||
{ "Plural-Forms:nplurals=3;plural=(n==1?0:(((n%100>19)||((n%100==0)&&(n!=0)))?2:1));", tinygettext::PluralForms(3, [](int n)-> unsigned int { return (n==1?0:(((n%100>19)||((n%100==0)&&(n!=0)))?2:1)); }) }, // ro
|
||||
{ "Plural-Forms:nplurals=4;plural=(n==1?0:(n%10>=2&&n%10<=4)&&(n%100<12||n%100>14)?1:n!=1&&(n%10>=0&&n%10<=1)||(n%10>=5&&n%10<=9)||(n%100>=12&&n%100<=14)?2:3);", tinygettext::PluralForms(4, [](int n)-> unsigned int { return (n==1?0:(n%10>=2&&n%10<=4)&&(n%100<12||n%100>14)?1:n!=1&&(n%10>=0&&n%10<=1)||(n%10>=5&&n%10<=9)||(n%100>=12&&n%100<=14)?2:3); }) }, // pl
|
||||
};
|
41
lib/tinygettext/update_plural_forms.py
Executable file
41
lib/tinygettext/update_plural_forms.py
Executable file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Usage: ./update_plural_forms.py /path/to/po
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
print('Usage: ./update_plural_forms.py /path/to/po')
|
||||
exit(-1)
|
||||
|
||||
pl_include = {}
|
||||
for po in os.listdir(sys.argv[1]):
|
||||
if not po.endswith('.po'):
|
||||
continue
|
||||
po_file = open(sys.argv[1] + '/' + po, 'r')
|
||||
for line in po_file:
|
||||
if 'Plural-Forms:' in line:
|
||||
no_space_line = line.strip().replace(
|
||||
' ', '').replace('"', '').replace('\\n', '')
|
||||
if no_space_line in pl_include:
|
||||
break
|
||||
pl = no_space_line.split(';')
|
||||
if len(pl) != 3:
|
||||
break
|
||||
pl_include[no_space_line] = ['tinygettext::PluralForms(' + pl[0].replace(
|
||||
'Plural-Forms:nplurals=', '') + \
|
||||
', [](int n)-> unsigned int { return ' + pl[1].replace(
|
||||
'plural=', '') + '; })', po.replace('.po', '')]
|
||||
break
|
||||
|
||||
f = open('src/plural_forms_generated.hpp', 'w')
|
||||
f.write('// Generated by update_plural_forms.py, do not edit\n')
|
||||
f.write('#include <unordered_map>\n')
|
||||
f.write('std::unordered_map<std::string, tinygettext::PluralForms> g_plural_forms = {\n')
|
||||
for key, value in pl_include.items():
|
||||
f.write('{')
|
||||
f.write(' "' + key + '", ' + value[0])
|
||||
f.write(' },' + ' // ' + value[1] + '\n')
|
||||
f.write('};\n')
|
Loading…
Reference in New Issue
Block a user