VR-Forces 5.0.3 Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rwTranslatableStringObject.h
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (c) 2022 MAK Technologies, Inc.
3  * All rights reserved.
4  ****************************************************************************/
5 
9 
10 #pragma once
11 
12 #include <vrfutil/vrfutilDefines.h>
14 
15 #include <vrfutil/iteratorBase.h>
16 #include <vrfutil/kinematicTools.h>
19 
20 #include <readerWriter/rwString.h>
21 #include <vlutil/vlStringUtil.h>
22 
23 #include <boost/function.hpp>
24 
25 class DtOutputStream;
27 
32 {
33 public:
34 
35  typedef boost::function<std::string (const char* module, const char* stringToTranslate)> TranslationFunction;
36 
38 
40  parentRegistry = 0 );
41 
43  DtReaderWriterRegistry* parentRegistry = 0 );
44 
46  DtReaderWriterRegistry* parentRegistry = 0 );
47 
50 
52 
56  virtual bool operator == ( const DtRwTranslatableStringObject& orig ) const;
57  virtual bool operator != ( const DtRwTranslatableStringObject& orig ) const;
58 
62  virtual const char* readerWriterType() const;
63 
65  static const char* theXmlType();
66 
67  virtual const char* xmlType() const
68  {
69  return theXmlType();
70  }
71 
73  virtual void printTo( DtOutputStream& ) const;
74 
75  virtual void addTranslatableString( const DtString&, const DtString& module = "DtRwTranslatableStringObject",
76  const DtString& originalString = "" );
77  virtual void addArgument( const DtString& );
78 
79  virtual void clear();
80 
81  virtual std::string toFormattedString( bool finalCR = true ) const;
82 
89  virtual std::vector<int> typesFrom( const std::string& fmt, std::string& reformatted ) const;
90 
92  static DtRwTranslatableStringObject trUtf8( const DtString& string );
93 
94  DtString translate( bool finalCR = true ) const
95  {
96  DtIteratorBase<DtRwString> iterator(myStringQueue);
97  DtString result;
98  unsigned int argsTranslated = 0;
99 
100  while (iterator.current())
101  {
102  std::vector<std::string> results;
103  std::string source(iterator.current()->name().c_str());
104 
105  oldBoostSplit(results, source, boost::algorithm::is_any_of("="));
106 
107  if (results.size())
108  {
109  if (results[0] == "translate")
110  {
111  std::string module;
112  std::string toTranslate = (*iterator.current()).c_str();
113  if (results.size() >= 2)
114  {
115  module = results[1];
116 
117  if (results.size() == 3)
118  {
119  toTranslate = results[2];
120  }
121  }
122 
127  std::string translated = (theTranslatorFunction)(module.c_str(), toTranslate.c_str()).c_str();
128 
129  if (translated == toTranslate.c_str())
130  {
131  translated = (*iterator.current()).c_str();
132  }
133 
134  result += translated;
135  }
136  else if (results[0] == "argument")
137  {
140  const unsigned MaxStrSize = 511;
141  const unsigned MaxTruncated = 508;
142  if (result.length() > MaxStrSize)
143  {
144  result = DtString(result.c_str(), MaxTruncated) + "...";
145  }
146  else if (result.length() + iterator.current()->length() > MaxStrSize)
147  {
148  unsigned maxSize = MaxTruncated - result.length();
149  DtString truncated(iterator.current()->c_str(), maxSize);
150  result = DtReplaceSubstring(result, "%1", truncated);
151  result += "...";
152  }
153  else
154  {
155  result = DtReplaceSubstring(result, "%1", iterator.current()->c_str());
156  }
157 
158  argsTranslated++;
159 
160  result = DtReplaceSubstring(result, "%" + DtString(argsTranslated + 1), "%1");
161  }
162  }
163  else
164  {
165  result += *iterator.current();
166  }
167 
168  ++iterator;
169  }
170 
171  if (finalCR && (!result.length() || result[result.length() - 1] != '\n'))
172  {
173  result += "\n";
174  }
175 
176  return result;
177  }
178 
179  static void setTranslationFunction(TranslationFunction f) { theTranslatorFunction = f; };
180  TranslationFunction translationFunction() const { return theTranslatorFunction; };
181 
182  static std::string defaultTranslationFunction(const char* module, const char* toTranslate) { return toTranslate; };
183 
184 protected:
187 };
bool operator==(const DtReaderWriter &) const
Equivalence operator.
virtual const char * readerWriterType() const
Used to supply a string type that this reader writer type is. This can be used in place of dynamic-ca...
static void setTranslationFunction(TranslationFunction f)
Definition: rwTranslatableStringObject.h:179
virtual T * current()
Definition: iteratorBase.h:330
DT_DLL_tdbutil DtString trUtf8(const char *text, const char *module)
Definition: readerWriter.h:85
Definition: readerWriterRegistry.h:39
boost::function< std::string(const char *module, const char *stringToTranslate)> TranslationFunction
Definition: rwTranslatableStringObject.h:35
DtString translate(bool finalCR=true) const
Definition: rwTranslatableStringObject.h:94
virtual const char * xmlType() const
Definition: rwTranslatableStringObject.h:67
static TranslationFunction theTranslatorFunction
Definition: rwTranslatableStringObject.h:186
Description: DtIteratorBase is the base class for iterators used to iterate over a DtList...
Definition: iteratorBase.h:32
TranslationFunction translationFunction() const
Definition: rwTranslatableStringObject.h:180
const char * source
Definition: lz4.h:442
const char * c_str() const
DtRwTranslatableStringObject is a simple class that will keep track of strings that can be translated...
Definition: rwTranslatableStringObject.h:31
SequenceSequenceT & oldBoostSplit(SequenceSequenceT &Result, RangeT &Input, PredicateT Pred, boost::algorithm::token_compress_mode_type eCompress=boost::algorithm::token_compress_off)
Replace boost split with version that returns an empty set if the supplied string has no length...
Definition: kinematicTools.h:941
#define DT_DLL_vrfutil
This file is used to determine how to build Disable inconsistent dll linkage warning Visual Studio 6...
Definition: vrfutilDefines.h:34
const DtReaderWriter & operator=(const DtReaderWriter &orig)
assignment operator
static const char * theXmlType()
The type that is used when archiving to an XML stream.
bool operator!=(const DtReaderWriter &rhs)
Definition: readerWriter.h:121
virtual DtSymbolString name() const
Accessor for name of this readable/writable object.

Document ID: Generated on Thu Jun 1 17:58:13 EDT 2023 from SVN revision 255404
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)