summaryrefslogtreecommitdiff
path: root/library/misc.cpp
blob: 5fbeb655ef5304cfd8599c1255b38bc8da124791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "misc.h"
#include <fstream>
#include <wx/msgdlg.h>
#include "resources.h"
#include "globalFunctions.h"


const string CustomLocale::FfsLanguageDat = "language.dat";


void exchangeEscapeChars(wxString& data)
{
    wxString output;

    const wxChar* input = data.c_str();

    wxChar value;
    while ((value = *input) != wxChar(0))
    {
        //read backslash
        if (value == wxChar('\\'))
        {
            //read next character
            ++input;
            value = *input;
            if (value == wxChar(0))
                break;

            switch (value)
            {
            case wxChar('\\'):
                output+= wxChar('\\');
                break;
            case wxChar('n'):
                output+= wxChar('\n');
                break;
            case wxChar('t'):
                output+= wxChar('\t');
                break;
            case wxChar('\"'):
                output+= wxChar('\"');
                break;
            default:
                output+= value;
            }
        }
        else
            output+= value;

        ++input;
    }
    data = output;
}


CustomLocale::CustomLocale() :
        wxLocale(),
        currentLanguage(wxLANGUAGE_ENGLISH)
{}


CustomLocale::~CustomLocale()
{
    //write language to file
    ofstream output(FfsLanguageDat.c_str());
    if (output)
    {
        globalFunctions::writeInt(output, currentLanguage);
        output.close();
    }
    else
        wxMessageBox(wxString(_("Could not write to ")) + wxT("\"") + wxString::From8BitData(FfsLanguageDat.c_str()) + wxT("\""), _("An exception occured!"), wxOK | wxICON_ERROR);
}


void CustomLocale::loadLanguageFromCfg()  //retrieve language from config file: do NOT put into constructor since working directory has not been set!
{
    int language = wxLANGUAGE_ENGLISH;

    //try to load language setting from file
    ifstream input(FfsLanguageDat.c_str());
    if (input)
    {
        language = globalFunctions::readInt(input);
        input.close();
    }
    else
        language = wxLocale::GetSystemLanguage();

    wxLocale::Init(language, wxLOCALE_LOAD_DEFAULT | wxLOCALE_CONV_ENCODING);

    loadLanguageFile(language);
}


void CustomLocale::loadLanguageFile(int language)
{
    currentLanguage = language;

    string languageFile;
    switch (language)
    {
    case wxLANGUAGE_GERMAN:
        languageFile = "german.lng";
        break;
    case wxLANGUAGE_FRENCH:
        languageFile = "french.lng";
        break;
    case wxLANGUAGE_JAPANESE:
        languageFile = "japanese.lng";
        break;
    default:
        languageFile.clear();
        currentLanguage = wxLANGUAGE_ENGLISH;
    }

    //load language file into buffer
    translationDB.clear();
    const int bufferSize = 100000;
    char temp[bufferSize];
    if (!languageFile.empty())
    {
        ifstream langFile(languageFile.c_str(), ios::binary);
        if (langFile)
        {
            int rowNumber = 0;
            TranslationLine currentLine;

            //Delimiter:
            //----------
            //Linux: 0xa        \n
            //Mac:   0xd        \r
            //Win:   0xd 0xa    \r\n    <- language files are in Windows format
            while (langFile.getline(temp, bufferSize, 0xd)) //specify delimiter explicitly
            {
                langFile.get(); //discard the 0xa character

                wxString formattedString = wxString::FromUTF8(temp);
                //wxString formattedString = wxString::From8BitData(temp);

                exchangeEscapeChars(formattedString);

                if (rowNumber%2 == 0)
                    currentLine.original = formattedString;
                else
                {
                    currentLine.translation = formattedString;
                    translationDB.insert(currentLine);
                }
                ++rowNumber;
            }
            langFile.close();
        }
        else
            wxMessageBox(wxString(_("Could not read language file ")) + wxT("\"") + wxString(languageFile.c_str(), wxConvUTF8) + wxT("\""), _("An exception occured!"), wxOK | wxICON_ERROR);
    }
    else
        ;   //if languageFile is empty language is defaulted to english

    //these global variables need to be redetermined on language selection
    GlobalResources::decimalPoint = _(".");
    GlobalResources::thousandsSeparator = _(",");
}


const wxChar* CustomLocale::GetString(const wxChar* szOrigString, const wxChar* szDomain) const
{
    TranslationLine currentLine;
    currentLine.original = szOrigString;

    //look for translation in buffer table
    Translation::iterator i;
    if ((i = translationDB.find(currentLine)) != translationDB.end())
        return (i->translation.c_str());
    //fallback
    return (szOrigString);
}

bgstack15