Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
gettext_plural_form.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5#include <string_view>
6#include <memory>
7#include <functional>
8
10{
11public:
12 using NumT = unsigned long;
13 using Function = std::function<NumT(NumT)>;
14 using Ptr = std::shared_ptr<GettextPluralForm>;
15
16 GettextPluralForm(std::wstring_view str);
17
18 size_t size() const
19 {
20 return nplurals;
21 };
22
23 // Note that this function does not perform any bounds check as the number of plural
24 // translations provided by the translation file may deviate from nplurals,
25 NumT operator()(const NumT n) const {
26 return func ? func(n) : 0;
27 }
28
29 operator bool() const
30 {
31 return nplurals > 0;
32 }
33
34 static Ptr parseHeaderLine(std::wstring_view str) {
35 return Ptr(new GettextPluralForm(str));
36 }
37private:
38 // The number of plural forms.
39 size_t nplurals = 0;
40
41 // The formula for determining the plural form based on the input value; see
42 // https://www.gnu.org/software/gettext/manual/html_node/Translating-plural-forms.html
43 // for details.
44 Function func = nullptr;
45};
Definition gettext_plural_form.h:10
std::function< NumT(NumT)> Function
Definition gettext_plural_form.h:13
size_t size() const
Definition gettext_plural_form.h:18
size_t nplurals
Definition gettext_plural_form.h:39
GettextPluralForm(std::wstring_view str)
Definition gettext_plural_form.cpp:196
static Ptr parseHeaderLine(std::wstring_view str)
Definition gettext_plural_form.h:34
Function func
Definition gettext_plural_form.h:44
unsigned long NumT
Definition gettext_plural_form.h:12
std::shared_ptr< GettextPluralForm > Ptr
Definition gettext_plural_form.h:14
NumT operator()(const NumT n) const
Definition gettext_plural_form.h:25