Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
MessageUtilities.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Linguistics_MessageUtilities_h_
5#define _Stroika_Foundation_Linguistics_MessageUtilities_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
10#include "Stroika/Foundation/Common/Common.h"
11#include "Stroika/Foundation/Common/KeyValuePair.h"
12#include "Stroika/Foundation/Containers/Sequence.h"
14
15/**
16 *
17 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
18 *
19 * TODO:
20 * @todo Very VERY primitive linguistic support, but this could easily evolve over time. Led has a bunch of
21 * linguistic code I could move here, and I'm sure I could dig up more...
22 */
23
25
28
29 struct IRep {
30 virtual ~IRep () = default;
31 virtual bool AppliesToThisLocale (const locale& l) const = 0;
32 virtual pair<String, optional<String>> RemoveTrailingSentencePunctuation (const String& msg) const = 0;
33 virtual String PluralizeNoun (const String& s, const optional<String>& sPlural, int count) const = 0;
34 virtual String MakeNounSingular (const String& s) const = 0;
35 };
36
37 /**
38 */
39 struct Impl_en : IRep {
40 virtual bool AppliesToThisLocale (const locale& l) const override;
41 virtual pair<String, optional<String>> RemoveTrailingSentencePunctuation (const String& msg) const override;
42 virtual String PluralizeNoun (const String& s, const optional<String>& sPlural, int count) const override;
43 virtual String MakeNounSingular (const String& s) const override;
44 };
45
46 /*
47 * Maps locales to MessageUtilities instances, that can be used to linguistically specifically update a message
48 *
49 * \note - methods come in two versions - one that takes a locale, and one that doesn't. If the locale is omitted,
50 * the default locale - locale{} - is used.
51 *
52 * \par Example Usage
53 * \code
54 * s = Linguistics::MessageUtilities::Manager::sThe.RemoveTrailingSentencePunctuation (s)
55 * \endcode
56 *
57 * \note \em Thread-Safety <a href="Thread-Safety.md#Internally-Synchronized-Thread-Safety">Internally-Synchronized-Thread-Safety</a>
58 */
59 class Manager {
60 public:
61 /**
62 */
63 static Manager sThe;
64
65 public:
66 Manager (const Containers::Sequence<shared_ptr<const IRep>>& utilObjs = {});
67
68 public:
69 /**
70 */
71 nonvirtual Manager& operator= (const Manager& rhs) = default;
72
73 public:
74 /**
75 * Return a legal handler to use - even if before or after main. But prefer picking one based on the current
76 * locale (or argument locale) and what is installed with Configuration if its available.
77 *
78 * This is generally unneeded, as you can call MakeNounSingular, RemoveTrailingSentencePunctuation directly (below).
79 * But if you have to make many calls in a row, caching the 'shared_ptr<const IRep>' - locale specific object - can improve
80 * performance.
81 *
82 * \note \em Thread-Safety <a href="Thread-Safety.md#Internally-Synchronized-Thread-Safety">Internally-Synchronized-Thread-Safety</a>
83 */
84 nonvirtual shared_ptr<const IRep> LookupHandler (const locale& l = locale{}) const;
85
86 public:
87 /**
88 * Uses a MessageUtilities based on the current thread's locale, if called between the start and end of main, and
89 * otherwise uses an arbitrary one.
90 *
91 * \par Example Usage
92 * \code
93 * s = Linguistics::MessageUtilities::Manager::sThe.RemoveTrailingSentencePunctuation (s)
94 * \endcode
95 */
96 nonvirtual pair<String, optional<String>> RemoveTrailingSentencePunctuation (const String& msg) const;
97 nonvirtual pair<String, optional<String>> RemoveTrailingSentencePunctuation (const locale& l, const String& msg) const;
98
99 public:
100 /**
101 * Implement current-ui-language-specific noun-pluralization logic for the given noun string (assuming the count of that noun
102 * is given (english rules - if count != 1 - append s, but we don't want that logic to proliferate through the app, so
103 * its easier to localize.
104 *
105 * The variation with two strings - the second one is the explicit plural - just plugged in if the count is non-zero
106 */
107 nonvirtual String PluralizeNoun (const String& s, int count = 1000) const;
108 nonvirtual String PluralizeNoun (const String& s, const String& sPlural, int count) const;
109 nonvirtual String PluralizeNoun (const locale& l, const String& s, int count = 1000) const;
110 nonvirtual String PluralizeNoun (const locale& l, const String& s, const String& sPlural, int count) const;
111
112 public:
113 /**
114 * Take argument string (assumed noun) munge it so its singular (if it happened to have been plural).
115 */
116 nonvirtual String MakeNounSingular (const locale& l, const String& s) const;
117 nonvirtual String MakeNounSingular (const String& s) const;
118
119 private:
122 };
123 inline Manager Manager::sThe;
124
125}
126
127/*
128 ********************************************************************************
129 ***************************** Implementation Details ***************************
130 ********************************************************************************
131 */
132#include "MessageUtilities.inl"
133
134#endif /*_Stroika_Foundation_Linguistics_MessageUtilities_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Definition Sequence.h:187
Wrap any object with Synchronized<> and it can be used similarly to the base type,...