MofileReader
moFileReader Class Reference

This class is a gettext-replacement. More...

#include <moFileReader.hpp>

+ Inheritance diagram for moFileReader:
+ Collaboration diagram for moFileReader:

Public Types

enum  eErrorCode {
  EC_SUCCESS = 0 , EC_ERROR , EC_FILENOTFOUND , EC_FILEINVALID ,
  EC_TABLEEMPTY , EC_MAGICNUMBER_NOMATCH , EC_MAGICNUMBER_REVERSED
}
 The possible errorcodes for methods of this class. More...
 

Public Member Functions

moFileReader::eErrorCode ParseData (const std::string &data)
 Reads a .mo-file. More...
 
eErrorCode ReadFile (const char *filename)
 Reads a .mo-file. More...
 
template<typename T >
eErrorCode ReadStream (T &stream)
 Reads data from a stream. More...
 
std::string Lookup (const char *id) const
 Returns the searched translation or returns the input. More...
 
std::string LookupWithContext (const char *context, const char *id) const
 Returns the searched translation or returns the input, restricted to the context given by context. See https://www.gnu.org/software/gettext/manual/html_node/Contexts.html for more info. More...
 
const std::string & GetErrorDescription () const
 Returns the Error Description. More...
 
void ClearTable ()
 Empties the Lookup-Table. More...
 
unsigned int GetNumStrings () const
 Returns the Number of Entries in our Lookup-Table. More...
 

Static Public Member Functions

static eErrorCode ExportAsHTML (const std::string &infile, const std::string &filename="", const std::string &css=g_css)
 Exports the whole content of the .mo-File as .html. More...
 

Static Public Attributes

static const unsigned int MagicNumber = 0x950412DE
 The Magic Number describes the endianess of bytes on the system. More...
 
static const unsigned int MagicReversed = 0xDE120495
 If the Magic Number is Reversed, we need to swap the bytes. More...
 
static const char ContextSeparator = '\x04'
 The character that is used to separate context strings. More...
 

Protected Types

typedef std::map< std::string, std::string > moLookupList
 Type for the map which holds the translation-pairs later. More...
 
typedef std::map< std::string, moLookupListmoContextLookupList
 Type for the 2D map which holds the translation-pairs later. More...
 

Protected Member Functions

unsigned long SwapBytes (unsigned long in)
 Swap the endianness of a 4 byte WORD. More...
 

Protected Attributes

std::string m_error
 Keeps the last error as String. More...
 

Detailed Description

This class is a gettext-replacement.

The usage is quite simple:
Tell the class which .mo-file it shall load via moFileReader::ReadFile(). The method will attempt to load the file, all translations will be stored in memory. Afterwards you can lookup the strings with moFileReader::Lookup() just like you would do with gettext. Additionally, you can call moFileReader::ReadFile() for as much files as you like. But please be aware, that if there are duplicated keys (original strings), that they will replace each other in the lookup-table. There is no check done, if a key already exists.

Note
If you add "Lookup" to the keywords of the gettext-parser (like poEdit), it will recognize the Strings loaded with an instance of this class.
I strongly recommend poEdit from Vaclav Slavik for editing .po-Files, get it at http://poedit.net for various systems :).

Definition at line 294 of file moFileReader.hpp.

Member Typedef Documentation

◆ moContextLookupList

typedef std::map<std::string, moLookupList> moFileReader::moContextLookupList
protected

Type for the 2D map which holds the translation-pairs later.

Definition at line 301 of file moFileReader.hpp.

◆ moLookupList

typedef std::map<std::string, std::string> moFileReader::moLookupList
protected

Type for the map which holds the translation-pairs later.

Definition at line 298 of file moFileReader.hpp.

Member Enumeration Documentation

◆ eErrorCode

The possible errorcodes for methods of this class.

Enumerator
EC_SUCCESS 

Indicated success.

EC_ERROR 

Indicates an error.

EC_FILENOTFOUND 

The given File was not found.

EC_FILEINVALID 

The file is invalid.

EC_TABLEEMPTY 

Empty Lookup-Table (returned by ExportAsHTML())

EC_MAGICNUMBER_NOMATCH 

The magic number did not match.

EC_MAGICNUMBER_REVERSED 

The magic number is reversed.

Note
This is an error until the class supports it.

Definition at line 314 of file moFileReader.hpp.

Member Function Documentation

◆ ClearTable()

void moFileReader::ClearTable ( )
inline

Empties the Lookup-Table.

Definition at line 563 of file moFileReader.hpp.

◆ ExportAsHTML()

static eErrorCode moFileReader::ExportAsHTML ( const std::string &  infile,
const std::string &  filename = "",
const std::string &  css = g_css 
)
inlinestatic

Exports the whole content of the .mo-File as .html.

Parameters
[in]infileThe .mo-File to export.
[in]filenameWhere to store the .html-file. If empty, the path and filename of the _infile with .html appended.
[in,out]cssThe css-script for the visual style of the file, in case you don't like mine ;).
See also
g_css for the possible and used css-values.

Definition at line 586 of file moFileReader.hpp.

◆ GetErrorDescription()

const std::string& moFileReader::GetErrorDescription ( ) const
inline

Returns the Error Description.

Definition at line 557 of file moFileReader.hpp.

◆ GetNumStrings()

unsigned int moFileReader::GetNumStrings ( ) const
inline

Returns the Number of Entries in our Lookup-Table.

Note
The mo-File-table always contains an empty msgid, which contains informations about the tranlsation-project. So the real number of strings is always minus 1.

Definition at line 574 of file moFileReader.hpp.

◆ Lookup()

std::string moFileReader::Lookup ( const char *  id) const
inline

Returns the searched translation or returns the input.

Parameters
[in]idThe id of the translation to search for.
Returns
The value you passed in via _id or the translated string.

Definition at line 531 of file moFileReader.hpp.

◆ LookupWithContext()

std::string moFileReader::LookupWithContext ( const char *  context,
const char *  id 
) const
inline

Returns the searched translation or returns the input, restricted to the context given by context. See https://www.gnu.org/software/gettext/manual/html_node/Contexts.html for more info.

Parameters
[in]contextRestrict to the context given.
[in]idThe id of the translation to search for.
Returns
The value you passed in via _id or the translated string.

Definition at line 545 of file moFileReader.hpp.

◆ ParseData()

moFileReader::eErrorCode moFileReader::ParseData ( const std::string &  data)
inline

Reads a .mo-file.

Parameters
[in]_filenameThe path to the file to load.
Returns
SUCCESS on success or one of the other error-codes in eErrorCode on error.

This is the core-feature. This method loads the .mo-file and stores all translation-pairs in a map. You can access this map via the method moFileReader::Lookup().

Definition at line 349 of file moFileReader.hpp.

◆ ReadFile()

eErrorCode moFileReader::ReadFile ( const char *  filename)
inline

Reads a .mo-file.

Parameters
[in]_filenameThe path to the file to load.
Returns
SUCCESS on success or one of the other error-codes in eErrorCode on error.

This is the core-feature. This method loads the .mo-file and stores all translation-pairs in a map. You can access this map via the method moFileReader::Lookup().

Definition at line 365 of file moFileReader.hpp.

◆ ReadStream()

template<typename T >
eErrorCode moFileReader::ReadStream ( T &  stream)
inline

Reads data from a stream.

Parameters
[in]stream
Returns
SUCCESS on success or one of the other error-codes in eErrorCode on error.

Definition at line 386 of file moFileReader.hpp.

◆ SwapBytes()

unsigned long moFileReader::SwapBytes ( unsigned long  in)
inlineprotected

Swap the endianness of a 4 byte WORD.

Parameters
[in]inThe value to swap.
Returns
The swapped value.

Definition at line 687 of file moFileReader.hpp.

Member Data Documentation

◆ ContextSeparator

const char moFileReader::ContextSeparator = '\x04'
static

The character that is used to separate context strings.

Definition at line 311 of file moFileReader.hpp.

◆ m_error

std::string moFileReader::m_error
protected

Keeps the last error as String.

Definition at line 681 of file moFileReader.hpp.

◆ MagicNumber

const unsigned int moFileReader::MagicNumber = 0x950412DE
static

The Magic Number describes the endianess of bytes on the system.

Definition at line 305 of file moFileReader.hpp.

◆ MagicReversed

const unsigned int moFileReader::MagicReversed = 0xDE120495
static

If the Magic Number is Reversed, we need to swap the bytes.

Definition at line 308 of file moFileReader.hpp.


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