Integrate Translator

This commit is contained in:
Jack Andersen 2015-12-30 17:19:03 -10:00
parent 23318a3e5d
commit a469a81260
7 changed files with 77 additions and 24 deletions

View File

@ -32,6 +32,7 @@ include_directories(include ${HECL_INCLUDE_DIR} ${BOO_INCLUDE_DIR}
list(APPEND SPECTER_HEADERS list(APPEND SPECTER_HEADERS
include/Specter/Specter.hpp include/Specter/Specter.hpp
include/Specter/ViewResources.hpp include/Specter/ViewResources.hpp
include/Specter/IViewManager.hpp
include/Specter/DeferredWindowEvents.hpp include/Specter/DeferredWindowEvents.hpp
include/Specter/View.hpp include/Specter/View.hpp
include/Specter/RootView.hpp include/Specter/RootView.hpp

View File

@ -8,6 +8,7 @@
#include "ScrollView.hpp" #include "ScrollView.hpp"
#include "Table.hpp" #include "Table.hpp"
#include "ViewResources.hpp" #include "ViewResources.hpp"
#include "IViewManager.hpp"
#include <HECL/HECL.hpp> #include <HECL/HECL.hpp>
namespace Specter namespace Specter
@ -68,13 +69,14 @@ private:
{ {
FileBrowser& m_fb; FileBrowser& m_fb;
ViewChild<std::unique_ptr<Button>> m_button; ViewChild<std::unique_ptr<Button>> m_button;
CancelButton(FileBrowser& fb, ViewResources& res) std::string m_text;
: m_fb(fb) CancelButton(FileBrowser& fb, ViewResources& res, const std::string& text)
: m_fb(fb), m_text(text)
{ {
m_button.m_view.reset(new Button(res, fb, this, "Cancel", Button::Style::Block, m_button.m_view.reset(new Button(res, fb, this, text, Button::Style::Block,
RectangleConstraint(100 * res.pixelFactor(), -1, RectangleConstraint::Test::Minimum))); RectangleConstraint(m_fb.m_ok.m_button.m_view->nominalWidth(), -1, RectangleConstraint::Test::Minimum)));
} }
const char* name() const {return "Cancel";} const char* name() const {return m_text.c_str();}
void activated(const boo::SWindowCoord&) {m_fb.cancelActivated();} void activated(const boo::SWindowCoord&) {m_fb.cancelActivated();}
} m_cancel; } m_cancel;
@ -101,8 +103,10 @@ private:
struct FileFieldBind : IStringBinding struct FileFieldBind : IStringBinding
{ {
FileBrowser& m_browser; FileBrowser& m_browser;
FileFieldBind(FileBrowser& browser) : m_browser(browser) {} std::string m_name;
const char* name() const {return "File Name";} FileFieldBind(FileBrowser& browser, const IViewManager& vm)
: m_browser(browser), m_name(vm.translateOr("file_name", "File Name")) {}
const char* name() const {return m_name.c_str();}
void changed(const std::string& val) void changed(const std::string& val)
{ {
} }
@ -119,9 +123,12 @@ private:
}; };
std::vector<Entry> m_entries; std::vector<Entry> m_entries;
std::string m_nameCol = "Name"; std::string m_nameCol;
std::string m_typeCol = "Type"; std::string m_typeCol;
std::string m_sizeCol = "Size"; std::string m_sizeCol;
std::string m_dirStr;
std::string m_fileStr;
size_t columnCount() const {return 3;} size_t columnCount() const {return 3;}
size_t rowCount() const {return m_entries.size();} size_t rowCount() const {return m_entries.size();}
@ -169,16 +176,25 @@ private:
HECL::SystemUTF8View nameUtf8(d.m_name); HECL::SystemUTF8View nameUtf8(d.m_name);
ent.m_name = nameUtf8.str(); ent.m_name = nameUtf8.str();
if (d.m_isDir) if (d.m_isDir)
ent.m_type = "Directory"; ent.m_type = m_dirStr;
else else
{ {
ent.m_type = "File"; ent.m_type = m_fileStr;
ent.m_size = HECL::HumanizeNumber(d.m_fileSz, 7, nullptr, int(HECL::HNScale::AutoScale), ent.m_size = HECL::HumanizeNumber(d.m_fileSz, 7, nullptr, int(HECL::HNScale::AutoScale),
HECL::HNFlags::B | HECL::HNFlags::Decimal); HECL::HNFlags::B | HECL::HNFlags::Decimal);
} }
} }
} }
FileListingDataBind(const IViewManager& vm)
{
m_nameCol = vm.translateOr("name", "Name");
m_typeCol = vm.translateOr("type", "Type");
m_sizeCol = vm.translateOr("size", "Size");
m_dirStr = vm.translateOr("directory", "Directory");
m_fileStr = vm.translateOr("file", "File");
}
} m_fileListingBind; } m_fileListingBind;
ViewChild<std::unique_ptr<Table>> m_fileListing; ViewChild<std::unique_ptr<Table>> m_fileListing;

View File

@ -0,0 +1,24 @@
#ifndef SPECTER_IVIEWMANAGER_HPP
#define SPECTER_IVIEWMANAGER_HPP
#include "Translator.hpp"
namespace Specter
{
struct IViewManager
{
public:
virtual const Translator* getTranslator() const {return nullptr;}
virtual std::string translateOr(const std::string& key, const char* vor) const
{
const Translator* trans = getTranslator();
if (trans)
return trans->translateOr(key, vor);
return vor;
}
};
}
#endif // SPECTER_IVIEWMANAGER_HPP

View File

@ -9,15 +9,12 @@
#include "Tooltip.hpp" #include "Tooltip.hpp"
#include "FontCache.hpp" #include "FontCache.hpp"
#include "DeferredWindowEvents.hpp" #include "DeferredWindowEvents.hpp"
#include "IViewManager.hpp"
#include <boo/boo.hpp> #include <boo/boo.hpp>
namespace Specter namespace Specter
{ {
struct IViewManager
{
};
class RootView : public View class RootView : public View
{ {
boo::IWindow* m_window = nullptr; boo::IWindow* m_window = nullptr;

View File

@ -12,12 +12,13 @@ class Locale
std::string m_name; std::string m_name;
std::string m_fullName; std::string m_fullName;
std::unique_ptr<Athena::io::YAMLNode> m_rootNode; std::unique_ptr<Athena::io::YAMLNode> m_rootNode;
const Athena::io::YAMLNode* m_langNode;
public: public:
Locale(const std::string& name, const std::string& fullName, Locale(const std::string& name, const std::string& fullName,
const unsigned char* yamlSource, size_t yamlLength); const unsigned char* yamlSource, size_t yamlLength);
const std::string& name() const {return m_name;} const std::string& name() const {return m_name;}
const std::string& fullName() const {return m_fullName;} const std::string& fullName() const {return m_fullName;}
const Athena::io::YAMLNode& rootNode() const {return *m_rootNode;} const Athena::io::YAMLNode& rootNode() const {return *m_langNode;}
}; };
class Translator class Translator
@ -26,7 +27,8 @@ class Translator
public: public:
Translator(const Locale* targetLocale) {setLocale(targetLocale);} Translator(const Locale* targetLocale) {setLocale(targetLocale);}
void setLocale(const Locale* targetLocale); void setLocale(const Locale* targetLocale);
const std::string* translate(const std::string& key); const std::string* translate(const std::string& key) const;
std::string translateOr(const std::string& key, const char* vor) const;
}; };
} }

View File

@ -49,23 +49,25 @@ FileBrowser::FileBrowser(ViewResources& res, View& parentView, const std::string
m_left(*this, res), m_left(*this, res),
m_right(*this, res), m_right(*this, res),
m_ok(*this, res, title), m_ok(*this, res, title),
m_cancel(*this, res), m_cancel(*this, res, rootView().viewManager().translateOr("cancel", "Cancel")),
m_fileFieldBind(*this) m_fileFieldBind(*this, rootView().viewManager()),
m_fileListingBind(rootView().viewManager())
{ {
commitResources(res); commitResources(res);
setBackground({0,0,0,0.5}); setBackground({0,0,0,0.5});
IViewManager& vm = rootView().viewManager();
m_fileField.m_view.reset(new TextField(res, *this, &m_fileFieldBind)); m_fileField.m_view.reset(new TextField(res, *this, &m_fileFieldBind));
m_fileListing.m_view.reset(new Table(res, *this, &m_fileListingBind)); m_fileListing.m_view.reset(new Table(res, *this, &m_fileListingBind));
m_systemBookmarks.m_view.reset(new Table(res, *this, &m_systemBookmarkBind)); m_systemBookmarks.m_view.reset(new Table(res, *this, &m_systemBookmarkBind));
m_systemBookmarksLabel.reset(new TextView(res, *this, res.m_mainFont)); m_systemBookmarksLabel.reset(new TextView(res, *this, res.m_mainFont));
m_systemBookmarksLabel->typesetGlyphs("System Locations:", res.themeData().uiText()); m_systemBookmarksLabel->typesetGlyphs(vm.translateOr("system_locations", "System Locations"), res.themeData().uiText());
m_projectBookmarks.m_view.reset(new Table(res, *this, &m_projectBookmarkBind)); m_projectBookmarks.m_view.reset(new Table(res, *this, &m_projectBookmarkBind));
m_projectBookmarksLabel.reset(new TextView(res, *this, res.m_mainFont)); m_projectBookmarksLabel.reset(new TextView(res, *this, res.m_mainFont));
m_projectBookmarksLabel->typesetGlyphs("Recent Projects:", res.themeData().uiText()); m_projectBookmarksLabel->typesetGlyphs(vm.translateOr("recent_projects", "Recent Projects"), res.themeData().uiText());
m_recentBookmarks.m_view.reset(new Table(res, *this, &m_recentBookmarkBind)); m_recentBookmarks.m_view.reset(new Table(res, *this, &m_recentBookmarkBind));
m_recentBookmarksLabel.reset(new TextView(res, *this, res.m_mainFont)); m_recentBookmarksLabel.reset(new TextView(res, *this, res.m_mainFont));
m_recentBookmarksLabel->typesetGlyphs("Recent Files:", res.themeData().uiText()); m_recentBookmarksLabel->typesetGlyphs(vm.translateOr("recent_files", "Recent Files"), res.themeData().uiText());
navigateToPath(initialPath); navigateToPath(initialPath);

View File

@ -15,6 +15,9 @@ Locale::Locale(const std::string& name, const std::string& fullName,
yaml_parser_set_input_string(&parser, yamlSource, yamlLength); yaml_parser_set_input_string(&parser, yamlSource, yamlLength);
reader.read(&parser); reader.read(&parser);
m_rootNode = std::move(reader.releaseRootNode()); m_rootNode = std::move(reader.releaseRootNode());
m_langNode = m_rootNode->findMapChild(name.c_str());
if (!m_langNode)
Log.report(LogVisor::FatalError, "no root node '%s' found in locale", name.c_str());
} }
void Translator::setLocale(const Locale* targetLocale) void Translator::setLocale(const Locale* targetLocale)
@ -44,9 +47,17 @@ static const std::string* RecursiveLookup(const Athena::io::YAMLNode& node,
return &ch->m_scalarString; return &ch->m_scalarString;
} }
const std::string* Translator::translate(const std::string& key) const std::string* Translator::translate(const std::string& key) const
{ {
return RecursiveLookup(m_targetLocale->rootNode(), key.cbegin(), key.cend()); return RecursiveLookup(m_targetLocale->rootNode(), key.cbegin(), key.cend());
} }
std::string Translator::translateOr(const std::string& key, const char* vor) const
{
const std::string* find = RecursiveLookup(m_targetLocale->rootNode(), key.cbegin(), key.cend());
if (find)
return *find;
return vor;
}
} }