Audacity  2.2.2
Static Public Member Functions | Static Private Attributes | List of all members
Internat Class Reference

Internationalisation support. More...

#include <Internat.h>

Static Public Member Functions

static void Init ()
 Initialize internationalisation support. Call this once at program start. More...
 
static wxChar GetDecimalSeparator ()
 Get the decimal separator for the current locale. More...
 
static bool CompatibleToDouble (const wxString &stringToConvert, double *result)
 Convert a string to a number. More...
 
static double CompatibleToDouble (const wxString &stringToConvert)
 
static wxString ToString (double numberToConvert, int digitsAfterDecimalPoint=-1)
 Convert a number to a string, always uses the dot as decimal separator. More...
 
static wxString ToDisplayString (double numberToConvert, int digitsAfterDecimalPoint=-1)
 Convert a number to a string, uses the user's locale's decimal separator. More...
 
static wxString FormatSize (wxLongLong size)
 Convert a number to a string while formatting it in bytes, KB, MB, GB. More...
 
static wxString FormatSize (double size)
 
static bool SanitiseFilename (wxString &name, const wxString &sub)
 Protect against Unicode to multi-byte conversion failures on Windows. More...
 
static wxString StripAccelerators (const wxString &str)
 Remove accelerator charactors from strings. More...
 
static const wxArrayString & GetExcludedCharacters ()
 

Static Private Attributes

static wxChar mDecimalSeparator = wxT('.')
 
static wxArrayString exclude
 
static wxCharBuffer mFilename
 

Detailed Description

Internationalisation support.

This class is used to help internationalisation and in general compatibility with different locales and character sets. It deals mostly with converting numbers, but also has important functions to convert to/from UTF-8, which is used in XML files and on Mac OS X for the filesystem.

Definition at line 86 of file Internat.h.

Member Function Documentation

bool Internat::CompatibleToDouble ( const wxString &  stringToConvert,
double *  result 
)
static

Convert a string to a number.

This function will accept BOTH point and comma as a decimal separator, regardless of the current locale. Returns 'true' on success, and 'false' if an error occurs.

Definition at line 122 of file Internat.cpp.

References GetDecimalSeparator().

Referenced by PCMAliasBlockFile::BuildFromXML(), SimpleBlockFile::BuildFromXML(), CompatibleToDouble(), NyquistEffect::GetCtrlValue(), SelectedRegion::HandleXMLAttribute(), EnvPoint::HandleXMLTag(), TimeTrack::HandleXMLTag(), ImportXMLTagHandler::HandleXMLTag(), EffectEqualization::HandleXMLTag(), WaveTrack::HandleXMLTag(), WaveClip::HandleXMLTag(), AudacityProject::HandleXMLTag(), LabelStruct::Import(), LOFImportFileHandle::lofOpenFiles(), LadspaEffect::OnTextCtrl(), EffectAmplify::PopulateOrExchange(), WrappedType::ReadAsDouble(), ViewInfo::ReadXMLAttribute(), and WrappedType::WriteToAsString().

123 {
124  // Regardless of the locale, always respect comma _and_ point
125  wxString s = stringToConvert;
126  s.Replace(wxT(","), wxString(GetDecimalSeparator()));
127  s.Replace(wxT("."), wxString(GetDecimalSeparator()));
128  return s.ToDouble(result);
129 }
static wxChar GetDecimalSeparator()
Get the decimal separator for the current locale.
Definition: Internat.cpp:117
double Internat::CompatibleToDouble ( const wxString &  stringToConvert)
static

Definition at line 131 of file Internat.cpp.

References CompatibleToDouble().

132 {
133  double result = 0;
134  Internat::CompatibleToDouble(stringToConvert, &result);
135  return result;
136 }
static bool CompatibleToDouble(const wxString &stringToConvert, double *result)
Convert a string to a number.
Definition: Internat.cpp:122
wxString Internat::FormatSize ( wxLongLong  size)
static

Convert a number to a string while formatting it in bytes, KB, MB, GB.

Definition at line 191 of file Internat.cpp.

Referenced by HistoryWindow::DoUpdate(), UndoManager::GetLongDescription(), DependencyDialog::OnCopyToClipboard(), DependencyDialog::PopulateList(), and DirectoriesPrefs::UpdateFreeSpace().

192 {
193  /* wxLongLong contains no built-in conversion to double */
194  double dSize = size.GetHi() * pow(2.0, 32); // 2 ^ 32
195  dSize += size.GetLo();
196 
197  return FormatSize(dSize);
198 }
static wxString FormatSize(wxLongLong size)
Convert a number to a string while formatting it in bytes, KB, MB, GB.
Definition: Internat.cpp:191
wxString Internat::FormatSize ( double  size)
static

Definition at line 200 of file Internat.cpp.

References _(), and ToDisplayString().

201 {
202  wxString sizeStr;
203 
204  if (size == -1)
205  sizeStr = _("Unable to determine");
206  else {
207  /* make it look nice, by formatting into k, MB, etc */
208  if (size < 1024.0)
209  sizeStr = ToDisplayString(size) + wxT(" ") + _("bytes");
210  else if (size < 1024.0 * 1024.0) {
211  /* i18n-hint: Abbreviation for Kilo bytes */
212  sizeStr = ToDisplayString(size / 1024.0, 1) + wxT(" ") + _("KB");
213  }
214  else if (size < 1024.0 * 1024.0 * 1024.0) {
215  /* i18n-hint: Abbreviation for Mega bytes */
216  sizeStr = ToDisplayString(size / (1024.0 * 1024.0), 1) + wxT(" ") + _("MB");
217  }
218  else {
219  /* i18n-hint: Abbreviation for Giga bytes */
220  sizeStr = ToDisplayString(size / (1024.0 * 1024.0 * 1024.0), 1) + wxT(" ") + _("GB");
221  }
222  }
223 
224  return sizeStr;
225 }
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
static wxString ToDisplayString(double numberToConvert, int digitsAfterDecimalPoint=-1)
Convert a number to a string, uses the user's locale's decimal separator.
Definition: Internat.cpp:149
wxChar Internat::GetDecimalSeparator ( )
static

Get the decimal separator for the current locale.

Normally, this is a decimal point ('.'), but e.g. Germany uses a comma (',').

Definition at line 117 of file Internat.cpp.

References mDecimalSeparator.

Referenced by CompatibleToDouble(), ToDisplayString(), and ToString().

118 {
119  return mDecimalSeparator;
120 }
static wxChar mDecimalSeparator
Definition: Internat.h:150
static const wxArrayString& Internat::GetExcludedCharacters ( )
inlinestatic

Definition at line 146 of file Internat.h.

References exclude.

Referenced by ExportMultiple::MakeFileName().

147  { return exclude; }
static wxArrayString exclude
Definition: Internat.h:153
void Internat::Init ( )
static

Initialize internationalisation support. Call this once at program start.

Definition at line 82 of file Internat.cpp.

References exclude, format, and mDecimalSeparator.

Referenced by AudacityApp::InitLang().

83 {
84  // Save decimal point character
85  struct lconv * localeInfo = localeconv();
86  if (localeInfo)
87  mDecimalSeparator = wxString(wxSafeConvertMB2WX(localeInfo->decimal_point)).GetChar(0);
88 
89 // wxLogDebug(wxT("Decimal separator set to '%c'"), mDecimalSeparator);
90 
91  // Setup list of characters that aren't allowed in file names
92  // Hey! The default wxPATH_NATIVE does not do as it should.
93 #if defined(__WXMAC__)
94  wxPathFormat format = wxPATH_MAC;
95 #elif defined(__WXGTK__)
96  wxPathFormat format = wxPATH_UNIX;
97 #elif defined(__WXMSW__)
98  wxPathFormat format = wxPATH_WIN;
99 #endif
100 
101  // This is supposed to return characters not permitted in paths to files
102  // or to directories
103  auto forbid = wxFileName::GetForbiddenChars(format);
104 
105  for(auto cc: forbid)
106  exclude.Add(wxString{ cc });
107 
108  // The path separators may not be forbidden, so add them
109  auto separators = wxFileName::GetPathSeparators(format);
110 
111  for(auto cc: separators) {
112  if (forbid.Find(cc) == wxNOT_FOUND)
113  exclude.Add(wxString{ cc });
114  }
115 }
int format
Definition: ExportPCM.cpp:56
static wxArrayString exclude
Definition: Internat.h:153
static wxChar mDecimalSeparator
Definition: Internat.h:150
bool Internat::SanitiseFilename ( wxString &  name,
const wxString &  sub 
)
static

Protect against Unicode to multi-byte conversion failures on Windows.

Check a proposed file name string for illegal characters and remove them return true iff name is "visibly" changed (not necessarily equivalent to character-wise changed)

Definition at line 267 of file Internat.cpp.

References exclude.

Referenced by ExportMultiple::MakeFileName().

268 {
269  bool result = false;
270  for(const auto &item : exclude)
271  {
272  if(name.Contains(item))
273  {
274  name.Replace(item, sub);
275  result = true;
276  }
277  }
278 
279 #ifdef __WXMAC__
280  // Special Mac stuff
281  // '/' is permitted in file names as seen in dialogs, even though it is
282  // the path separator. The "real" filename as seen in the terminal has ':'.
283  // Do NOT return true if this is the only subsitution.
284  name.Replace(wxT("/"), wxT(":"));
285 #endif
286 
287  return result;
288 }
static wxArrayString exclude
Definition: Internat.h:153
const wxChar * name
Definition: Distortion.cpp:94
wxString Internat::StripAccelerators ( const wxString &  str)
static

Remove accelerator charactors from strings.

Utility function - takes a translatable string to be used as a menu item, for example _("&Splash...\tAlt+S"), and strips all of the menu accelerator stuff from it, to make "Splash". That way the same translatable string can be used both when accelerators are needed and when they aren't, saving translators effort.

Definition at line 290 of file Internat.cpp.

291 {
292  wxString result;
293  result.Alloc(s.Length());
294  for(size_t i = 0; i < s.Length(); i++) {
295  if (s[i] == '\t')
296  break;
297  if (s[i] != '&' && s[i] != '.')
298  result += s[i];
299  }
300  return result;
301 }
wxString Internat::ToDisplayString ( double  numberToConvert,
int  digitsAfterDecimalPoint = -1 
)
static

Convert a number to a string, uses the user's locale's decimal separator.

Definition at line 149 of file Internat.cpp.

References format, and GetDecimalSeparator().

Referenced by FormatSize(), EffectAutoDuckPanel::OnPaint(), LadspaEffect::OnSlider(), LadspaEffect::PopulateUI(), LadspaEffect::RefreshControls(), and ToString().

151 {
152  wxString decSep = wxString(GetDecimalSeparator());
153  wxString result;
154 
155  if (digitsAfterDecimalPoint == -1)
156  {
157  result.Printf(wxT("%f"), numberToConvert);
158 
159  // Not all libcs respect the decimal separator, so always convert
160  // any dots found to the decimal separator.
161  result.Replace(wxT("."), decSep);
162 
163  if (result.Find(decSep) != -1)
164  {
165  // Strip trailing zeros, but leave one, and decimal separator.
166  int pos = result.Length() - 1;
167  while ((pos > 1) &&
168  (result.GetChar(pos) == wxT('0')) &&
169  (result.GetChar(pos - 1) != decSep))
170  pos--;
171  // (Previous code removed all of them and decimal separator.)
172  // if (result.GetChar(pos) == decSep)
173  // pos--; // strip point before empty fractional part
174  result = result.Left(pos+1);
175  }
176  }
177  else
178  {
179  wxString format;
180  format.Printf(wxT("%%.%if"), digitsAfterDecimalPoint);
181  result.Printf(format, numberToConvert);
182 
183  // Not all libcs respect the decimal separator, so always convert
184  // any dots found to the decimal separator
185  result.Replace(wxT("."), decSep);
186  }
187 
188  return result;
189 }
int format
Definition: ExportPCM.cpp:56
static wxChar GetDecimalSeparator()
Get the decimal separator for the current locale.
Definition: Internat.cpp:117
wxString Internat::ToString ( double  numberToConvert,
int  digitsAfterDecimalPoint = -1 
)
static

Convert a number to a string, always uses the dot as decimal separator.

Definition at line 138 of file Internat.cpp.

References GetDecimalSeparator(), and ToDisplayString().

Referenced by LabelStruct::Export(), Sequence::HandleXMLEndTag(), Sequence::Paste(), EffectAmplify::PopulateOrExchange(), NyquistEffect::Process(), NyquistEffect::ProcessOne(), XMLWriter::WriteAttr(), and Sequence::WriteXML().

140 {
141  wxString result = ToDisplayString(
142  numberToConvert, digitsAfterDecimalPoint);
143 
144  result.Replace(wxString(GetDecimalSeparator()), wxT("."));
145 
146  return result;
147 }
static wxString ToDisplayString(double numberToConvert, int digitsAfterDecimalPoint=-1)
Convert a number to a string, uses the user's locale's decimal separator.
Definition: Internat.cpp:149
static wxChar GetDecimalSeparator()
Get the decimal separator for the current locale.
Definition: Internat.cpp:117

Member Data Documentation

wxArrayString Internat::exclude
staticprivate

Definition at line 153 of file Internat.h.

Referenced by GetExcludedCharacters(), Init(), and SanitiseFilename().

wxChar Internat::mDecimalSeparator = wxT('.')
staticprivate

Definition at line 150 of file Internat.h.

Referenced by GetDecimalSeparator(), and Init().

wxCharBuffer Internat::mFilename
staticprivate

Definition at line 155 of file Internat.h.


The documentation for this class was generated from the following files: