| 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 | #include <database/locale.hh> |
| 38 | #include <util/sqlitepp.hh> |
| 39 | #include <sqlite3.h> |
| 40 | #include <iostream> |
| 41 | |
| 42 | namespace db { |
| 43 | Locale::Locale(sqlitepp::Instance *db) : m_DB(db) { |
| 44 | { |
| 45 | sqlitepp::Stmt stmt(db->GetDB()); |
| 46 | stmt.Prepare("SELECT id, name, code FROM languages ORDER BY id ASC;"); |
| 47 | |
| 48 | while (stmt.Step() == SQLITE_ROW) { |
| 49 | Languages lang; |
| 50 | lang.id = stmt.ColumnInt(0); |
| 51 | lang.name = stmt.ColumnText(1); |
| 52 | lang.code = stmt.ColumnText(2); |
| 53 | |
| 54 | sqlitepp::Stmt langStmt(db->GetDB()); |
| 55 | langStmt.Prepare("SELECT key, value FROM locale WHERE language_id = ?;"); |
| 56 | langStmt.BindInt(1, lang.id); |
| 57 | |
| 58 | while (langStmt.Step() == SQLITE_ROW) |
| 59 | lang.dict.emplace(langStmt.ColumnText(0), langStmt.ColumnText(1)); |
| 60 | |
| 61 | m_Langs.emplace_back(std::move(lang)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | setLang(); |
| 66 | } |
| 67 | |
| 68 | string Locale::GetWord(const string &key) const { |
| 69 | auto it = m_CurLang.dict.find(key); |
| 70 | return it != m_CurLang.dict.end() ? it->second : key; |
| 71 | } |
| 72 | |
| 73 | void Locale::SetLanguage(i32 id) { |
| 74 | sqlitepp::Stmt stmt(m_DB->GetDB()); |
| 75 | stmt.Prepare("UPDATE settings SET value = ? WHERE key = ?;"); |
| 76 | stmt.BindInt(1, id); |
| 77 | stmt.BindText(2, "locale"); |
| 78 | |
| 79 | if (stmt.Step() != SQLITE_DONE) { |
| 80 | std::cerr << "言語変更に失敗。" << std::endl; |
| 81 | return; |
| 82 | } |
| 83 | setLang(); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | void Locale::setLang() noexcept { |
| 89 | sqlitepp::Stmt stmt(m_DB->GetDB()); |
| 90 | stmt.Prepare("SELECT value FROM settings WHERE key = ?;"); |
| 91 | stmt.BindText(1, "locale"); |
| 92 | |
| 93 | if ((stmt.Step() == SQLITE_ROW)) { |
| 94 | i32 curLangId = stmt.ColumnInt(0); |
| 95 | for (const auto &lang : m_Langs) { |
| 96 | if (lang.id == curLangId) { |
| 97 | m_CurLang = lang; |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |