Welcome to mirror list, hosted at ThFree Co, Russian Federation.

languages.cpp « map - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0024e0db637975bf1eecd3b1011585a57f95a389 (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
#include "languages.hpp"

#include "../defines.hpp"

#include "../platform/platform.hpp"
#include "../platform/preferred_languages.hpp"
#include "../platform/settings.hpp"

#include "../coding/file_reader.hpp"
#include "../coding/multilang_utf8_string.hpp"

#include "../base/logging.hpp"
#include "../base/string_utils.hpp"

#include "../std/algorithm.hpp"
#include "../std/sstream.hpp"


#define DEFAULT_LANGUAGES "default"
#define LANGUAGES_FILE "languages.txt"
#define LANG_DELIMETER "|"
#define SETTING_LANG_KEY "languages_priority"


namespace languages
{
  static int8_t gDefaultPriorities[] =
  {
    0, 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
  };

  int8_t const * GetCurrentPriorities()
  {
    return gDefaultPriorities;
  }

  static void SetPreferableLanguages(vector<string> const & langCodes)
  {
    CHECK_EQUAL(langCodes.size(), MAX_SUPPORTED_LANGUAGES, ());
    for (size_t i = 0; i < langCodes.size(); ++i)
    {
      int8_t const index = StringUtf8Multilang::GetLangIndex(langCodes[i]);
      if (index >= 0)
        gDefaultPriorities[index] = i;
      else
      {
        ASSERT(false, ("Invalid language code"));
      }
      CHECK_GREATER_OR_EQUAL(gDefaultPriorities[i], 0, ("Unsupported language", langCodes[i]));
    }
  }

  /// sorts outLanguages according to langCodes order
  static void Sort(vector<string> const & langCodes, CodesAndNamesT & outLanguages)
  {
    CodesAndNamesT result;
    for (size_t i = 0; i < langCodes.size(); ++i)
    {
      for (CodesAndNamesT::iterator it = outLanguages.begin(); it != outLanguages.end(); ++it)
      {
        if (langCodes[i] == it->first)
        {
          result.push_back(*it);
          outLanguages.erase(it);
          break;
        }
      }
    }
    // copy all languages left
    for (CodesAndNamesT::iterator it = outLanguages.begin(); it != outLanguages.end(); ++it)
      result.push_back(*it);

    result.swap(outLanguages);
    CHECK_EQUAL(outLanguages.size(), MAX_SUPPORTED_LANGUAGES, ());
  }

  struct Collector
  {
    CodesT & m_out;
    Collector(CodesT & out) : m_out(out) {}
    void operator()(string const & str)
    {
      m_out.push_back(str);
    }
  };

  void GetCurrentSettings(CodesT & outLangCodes)
  {
    CodesAndNamesT res;
    GetCurrentSettings(res);
    outLangCodes.clear();
    for (CodesAndNamesT::iterator it = res.begin(); it != res.end(); ++it)
      outLangCodes.push_back(it->first);
  }

  void GetCurrentSettings(CodesAndNamesT & outLanguages)
  {
    string settingsString;
    // get preffered languages from the system
    if (!Settings::Get(SETTING_LANG_KEY, settingsString))
      settingsString = languages::PreferredLanguages();

    CodesT currentCodes;
    Collector c(currentCodes);
    strings::Tokenize(settingsString, LANG_DELIMETER, c);

    GetSupportedLanguages(outLanguages);
    Sort(currentCodes, outLanguages);
  }

  void SaveSettings(CodesT const & langs)
  {
    CHECK_EQUAL(langs.size(), MAX_SUPPORTED_LANGUAGES, ());
    string const saveString = strings::JoinStrings(langs.begin(), langs.end(), LANG_DELIMETER);
    Settings::Set(SETTING_LANG_KEY, saveString);

    // apply new settings
    SetPreferableLanguages(langs);
  }

  bool GetSupportedLanguages(CodesAndNamesT & outLanguages)
  {
    outLanguages.clear();

    string buffer;
    ReaderPtr<Reader>(GetPlatform().GetReader(LANGUAGES_FILE)).ReadAsString(buffer);
    istringstream stream(buffer);

    for (size_t i = 0; i < MAX_SUPPORTED_LANGUAGES; ++i)
    {
      string line;
      getline(stream, line);
      size_t delimIndex = string::npos;
      if ((delimIndex = line.find(LANG_DELIMETER)) != string::npos)
        outLanguages.push_back(make_pair(line.substr(0, delimIndex), line.substr(delimIndex + 1)));
    }
    return !outLanguages.empty();
  }
}