tb_language.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_language.h"
  6. #include "tb_system.h"
  7. #include "tb_node_tree.h"
  8. namespace tb {
  9. TBLanguage::~TBLanguage()
  10. {
  11. Clear();
  12. }
  13. bool TBLanguage::Load(const char *filename)
  14. {
  15. // Read the file into a node tree (even though it's only a flat list)
  16. TBNode node;
  17. if (!node.ReadFile(filename))
  18. return false;
  19. // Go through all nodes and add to the strings hash table
  20. TBNode *n = node.GetFirstChild();
  21. while (n)
  22. {
  23. const char *str = n->GetValue().GetString();
  24. TBStr *new_str = new TBStr(str);
  25. if (!new_str || !strings.Add(TBID(n->GetName()), new_str))
  26. {
  27. delete new_str;
  28. return false;
  29. }
  30. n = n->GetNext();
  31. }
  32. return true;
  33. }
  34. void TBLanguage::Clear()
  35. {
  36. strings.DeleteAll();
  37. }
  38. const char *TBLanguage::GetString(const TBID &id)
  39. {
  40. if (TBStr *str = strings.Get(id))
  41. return *str;
  42. #ifdef TB_RUNTIME_DEBUG_INFO
  43. static TBStr tmp;
  44. tmp.SetFormatted("<TRANSLATE:%s>", id.debug_string.CStr());
  45. return tmp;
  46. #else
  47. return "<TRANSLATE!>";
  48. #endif
  49. }
  50. }; // namespace tb