|
85 | 85 | * [Translators](#translators) |
86 | 86 | * [Repository of translators](#repository-of-translators) |
87 | 87 | * [Adding new locale](#adding-new-locale) |
| 88 | + * [Localization example](#localization-example) |
88 | 89 | * [Building and installation](#building-and-installation) |
89 | 90 | * [Supported platforms and compilers](#supported-platforms-and-compilers) |
90 | 91 | * [Dependencies](#dependencies) |
@@ -2176,6 +2177,103 @@ m["value"]={ |
2176 | 2177 |
|
2177 | 2178 | Another example of custom locale can be found in `validator/reporting/locale/ru.hpp` that implements translations for Russian locale. |
2178 | 2179 |
|
| 2180 | +#### Localization example |
| 2181 | + |
| 2182 | +Below is an example of custom localization for Russian language. This example uses some grammatical categories of Russian language listed in `grammar_ru` enumeration. |
| 2183 | + |
| 2184 | +```cpp |
| 2185 | +#include <iostream> |
| 2186 | +#include <dracosha/validator/validator.hpp> |
| 2187 | +#include <dracosha/validator/reporting/extend_translator.hpp> |
| 2188 | +#include <dracosha/validator/reporting/locale/ru.hpp> |
| 2189 | + |
| 2190 | +using namespace DRACOSHA_VALIDATOR_NAMESPACE; |
| 2191 | + |
| 2192 | +// define custom trasnlator of container keys for Russian language taking into account some Russian grammatic categories |
| 2193 | +phrase_translator custom_key_translator; |
| 2194 | +custom_key_translator["password"]={ |
| 2195 | + {"пароль"}, |
| 2196 | + {"пароля",grammar_ru::roditelny_padezh} |
| 2197 | + }; |
| 2198 | +custom_key_translator["hyperlink"]={ |
| 2199 | + {{"гиперссылка",grammar_ru::zhensky_rod}}, |
| 2200 | + {{"гиперссылки",grammar_ru::zhensky_rod},grammar_ru::roditelny_padezh} |
| 2201 | + }; |
| 2202 | +custom_key_translator["words"]={ |
| 2203 | + {{"слова",grammar_ru::mn_chislo}} |
| 2204 | + }; |
| 2205 | + |
| 2206 | +/* |
| 2207 | +final translator merges predefined Russian translator |
| 2208 | +validator_translator_ru() and custom translator for element names defined above |
| 2209 | +*/ |
| 2210 | +auto final_key_translator=extend_translator(validator_translator_ru(),custom_key_translator); |
| 2211 | + |
| 2212 | +// container to validate |
| 2213 | +std::map<std::string,std::string> m1={ |
| 2214 | + {"password","123456"}, |
| 2215 | + {"hyperlink","zzzzzzzzz"} |
| 2216 | +}; |
| 2217 | + |
| 2218 | +// define reporting adapter that will generate error messages in Russian language |
| 2219 | +std::string rep; |
| 2220 | +auto ra1=make_reporting_adapter(m1,make_reporter(rep,make_formatter(final_key_translator))); |
| 2221 | + |
| 2222 | +// examples of error generation in Russian language with the reporting adapter defined above |
| 2223 | + |
| 2224 | +auto v1=validator( |
| 2225 | + _["words"](exists,true) |
| 2226 | + ); |
| 2227 | +if (!v1.apply(ra1)) |
| 2228 | +{ |
| 2229 | + std::cerr<<rep<<std::endl; |
| 2230 | + /* |
| 2231 | + prints: |
| 2232 | + "слова должны существовать" |
| 2233 | + */ |
| 2234 | +} |
| 2235 | +rep.clear(); |
| 2236 | + |
| 2237 | +auto v2=validator( |
| 2238 | + _["hyperlink"](eq,"https://www.boost.org") |
| 2239 | + ); |
| 2240 | +if (!v2.apply(ra1)) |
| 2241 | +{ |
| 2242 | + std::cerr<<rep<<std::endl; |
| 2243 | + /* |
| 2244 | + prints: |
| 2245 | + "гиперссылка должна быть равна https://www.boost.org" |
| 2246 | + */ |
| 2247 | +} |
| 2248 | +rep.clear(); |
| 2249 | + |
| 2250 | +auto v3=validator( |
| 2251 | + _["password"](length(gt,7)) |
| 2252 | + ); |
| 2253 | +if (!v3.apply(ra1)) |
| 2254 | +{ |
| 2255 | + std::cerr<<rep<<std::endl; |
| 2256 | + /* |
| 2257 | + prints: |
| 2258 | + "длина пароля должна быть больше 7" |
| 2259 | + */ |
| 2260 | +} |
| 2261 | +rep.clear(); |
| 2262 | + |
| 2263 | +auto v4=validator( |
| 2264 | + _["hyperlink"](length(lte,7)) |
| 2265 | + ); |
| 2266 | +if (!v4.apply(ra1)) |
| 2267 | +{ |
| 2268 | + std::cerr<<rep<<std::endl; |
| 2269 | + /* |
| 2270 | + prints: |
| 2271 | + "длина гиперссылки должна быть меньше или равна 7" |
| 2272 | + */ |
| 2273 | +} |
| 2274 | +rep.clear(); |
| 2275 | +``` |
| 2276 | + |
2179 | 2277 | # Building and installation |
2180 | 2278 |
|
2181 | 2279 | `cpp-validator` is a header-only library, so no special library building is required. Still, some extra configuration may be required when using the library. |
|
0 commit comments