VR-Forces 4.8 Class Documentation
 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) 2014 MaK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 
6 #pragma once
7 
10 
12 
13 #include "vrfutil/vrfutilDefines.h"
16 #include "vrfutil/iteratorBase.h"
17 #include <readerWriter/rwString.h>
18 #include "vrfutil/kinematicTools.h"
20 #include <boost/function.hpp>
21 #include <vlutil/vlStringUtil.h>
22 
23 class DtOutputStream;
24 
29 {
30 public:
31  typedef boost::function<std::string (const char* module, const char* stringToTranslate)> TranslationFunction;
32 
34 
36  parentRegistry = 0);
37 
39  DtReaderWriterRegistry* parentRegistry = 0);
40 
42  DtReaderWriterRegistry* parentRegistry = 0);
43 
45 
49  virtual bool operator==(const DtRwTranslatableStringObject& orig) const;
50  virtual bool operator!=(const DtRwTranslatableStringObject& orig) const;
51 
53 
57  virtual const char* readerWriterType() const;
58 
60  static const char* theXmlType();
61 
62  virtual const char* xmlType() const
63  {
64  return theXmlType();
65  }
66 
68  virtual void printTo(DtOutputStream&) const;
69 
70  virtual void addTranslatableString(const DtString&, const DtString& module = "DtRwTranslatableStringObject",
71  const DtString& originalString = "");
72  virtual void addArgument(const DtString&);
73 
74  virtual void clear();
75 
76  virtual std::string toFormattedString(bool finalCR = true) const;
77 
84  virtual std::vector<int> typesFrom(const std::string& fmt, std::string& reformatted) const;
85 
87  static DtRwTranslatableStringObject trUtf8(const DtString& string);
88 
89  DtString translate(bool finalCR = true) const
90  {
91  DtIteratorBase<DtRwString> iterator(myStringQueue);
92  DtString result;
93  unsigned int argsTranslated = 0;
94 
95  while (iterator.current())
96  {
97  std::vector<std::string> results;
98  std::string source(iterator.current()->name().c_str());
99 
100  oldBoostSplit(results, source, boost::algorithm::is_any_of("="));
101 
102  if (results.size())
103  {
104  if (results[0] == "translate")
105  {
106  std::string module;
107  std::string toTranslate = (*iterator.current()).c_str();
108  if (results.size() >= 2)
109  {
110  module = results[1];
111 
112  if (results.size() == 3)
113  {
114  toTranslate = results[2];
115  }
116  }
117 
122  std::string translated = (theTranslatorFunction)(module.c_str(), toTranslate.c_str()).c_str();
123 
124  if (translated == toTranslate.c_str())
125  {
126  translated = (*iterator.current()).c_str();
127  }
128 
129  result += translated;
130  }
131  else if (results[0] == "argument")
132  {
135  const unsigned MaxStrSize = 511;
136  const unsigned MaxTruncated = 508;
137  if (result.length() > MaxStrSize)
138  {
139  result = DtString(result.c_str(), MaxTruncated) + "...";
140  }
141  else if (result.length() + iterator.current()->length() > MaxStrSize)
142  {
143  unsigned maxSize = MaxTruncated - result.length();
144  DtString truncated(iterator.current()->c_str(), maxSize);
145  result = DtReplaceSubstring(result, "%1", truncated);
146  result += "...";
147  }
148  else
149  {
150  result = DtReplaceSubstring(result, "%1", iterator.current()->c_str());
151  }
152 
153  argsTranslated++;
154 
155  result = DtReplaceSubstring(result, "%" + DtString(argsTranslated + 1), "%1");
156  }
157  }
158  else
159  {
160  result += *iterator.current();
161  }
162 
163  ++iterator;
164  }
165 
166  if (finalCR && (!result.length() || result[result.length() - 1] != '\n'))
167  {
168  result += "\n";
169  }
170 
171  return result;
172  }
173 
174  static void setTranslationFunction(TranslationFunction f) { theTranslatorFunction = f; };
175  TranslationFunction translationFunction() const { return theTranslatorFunction; };
176 
177  static std::string defaultTranslationFunction(const char* module, const char* toTranslate) { return toTranslate; };
178 
179 protected:
182 };
bool operator==(const DtReaderWriter &) const
Equivalence operator.
virtual const char * readerWriterType() const
Used to supply a string type that this reader writer type is.
static void setTranslationFunction(TranslationFunction f)
Definition: rwTranslatableStringObject.h:174
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:31
DtString translate(bool finalCR=true) const
Definition: rwTranslatableStringObject.h:89
virtual const char * xmlType() const
Definition: rwTranslatableStringObject.h:62
static TranslationFunction theTranslatorFunction
Definition: rwTranslatableStringObject.h:181
Description: DtIteratorBase is the base class for iterators used to iterate over a DtList...
Definition: iteratorBase.h:32
TranslationFunction translationFunction() const
Definition: rwTranslatableStringObject.h:175
const char * source
Definition: lz4.h:436
const char * c_str() const
DtRwTranslatableStringObject is a simple class that will keep track of strings that can be translated...
Definition: rwTranslatableStringObject.h:28
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:946
#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 Aug 27 10:56:05 EDT 2020 from SVN revision 217100
Copyright © 2005-2020 MAK Technologies. All Rights Reserved (www.mak.com)