translation_loader_po.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*************************************************************************/
  2. /* translation_loader_po.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "translation_loader_po.h"
  30. #include "os/file_access.h"
  31. #include "translation.h"
  32. RES TranslationLoaderPO::load(const String &p_path,const String& p_original_path) {
  33. FileAccess *f=FileAccess::open(p_path,FileAccess::READ);
  34. ERR_FAIL_COND_V(!f,RES());
  35. String l = f->get_line();
  36. enum Status {
  37. STATUS_NONE,
  38. STATUS_READING_ID,
  39. STATUS_READING_STRING,
  40. };
  41. Status status=STATUS_NONE;
  42. String msg_id;
  43. String msg_str;
  44. String config;
  45. Ref<Translation> translation = Ref<Translation>( memnew( Translation ));
  46. int line = 1;
  47. while(true) {
  48. String l = f->get_line();
  49. if (f->eof_reached()) {
  50. if ( status == STATUS_READING_STRING) {
  51. if (msg_id!="")
  52. translation->add_message(msg_id,msg_str);
  53. else if (config=="")
  54. config=msg_str;
  55. break;
  56. } else if ( status==STATUS_NONE)
  57. break;
  58. memdelete(f);
  59. ERR_EXPLAIN(p_path+":"+itos(line)+" Unexpected EOF while reading 'msgid' at file: ");
  60. ERR_FAIL_V(RES());
  61. }
  62. l=l.strip_edges();
  63. if (l.begins_with("msgid")) {
  64. if (status==STATUS_READING_ID) {
  65. memdelete(f);
  66. ERR_EXPLAIN(p_path+":"+itos(line)+" nexpected 'msgid', was expecting 'msgstr' while parsing: ");
  67. ERR_FAIL_V(RES());
  68. }
  69. if (msg_id!="")
  70. translation->add_message(msg_id,msg_str);
  71. else if (config=="")
  72. config=msg_str;
  73. l=l.substr(5,l.length()).strip_edges();
  74. status=STATUS_READING_ID;
  75. msg_id="";
  76. msg_str="";
  77. }
  78. if (l.begins_with("msgstr")) {
  79. if (status!=STATUS_READING_ID) {
  80. memdelete(f);
  81. ERR_EXPLAIN(p_path+":"+itos(line)+" Unexpected 'msgstr', was expecting 'msgid' while parsing: ");
  82. ERR_FAIL_V(RES());
  83. }
  84. l=l.substr(6,l.length()).strip_edges();
  85. status=STATUS_READING_STRING;
  86. }
  87. if (l=="" || l.begins_with("#")) {
  88. line++;
  89. continue; //nothing to read or comment
  90. }
  91. if (!l.begins_with("\"") || status==STATUS_NONE) {
  92. //not a string? failure!
  93. ERR_EXPLAIN(p_path+":"+itos(line)+" Invalid line '"+l+"' while parsing: ");
  94. ERR_FAIL_V(RES());
  95. }
  96. l=l.substr(1,l.length());
  97. //find final quote
  98. int end_pos=-1;
  99. for(int i=0;i<l.length();i++) {
  100. if (l[i]=='"' && (i==0 || l[i-1]!='\\')) {
  101. end_pos=i;
  102. break;
  103. }
  104. }
  105. if (end_pos==-1) {
  106. ERR_EXPLAIN(p_path+":"+itos(line)+" Expected '\"' at end of message while parsing file: ");
  107. ERR_FAIL_V(RES());
  108. }
  109. l=l.substr(0,end_pos);
  110. l=l.c_unescape();
  111. if (status==STATUS_READING_ID)
  112. msg_id+=l;
  113. else
  114. msg_str+=l;
  115. line++;
  116. }
  117. f->close();
  118. memdelete(f);
  119. if (config=="") {
  120. ERR_EXPLAIN("No config found in file: "+p_path);
  121. ERR_FAIL_V(RES());
  122. }
  123. Vector<String> configs = config.split("\n");
  124. for(int i=0;i<configs.size();i++) {
  125. String c = configs[i].strip_edges();
  126. int p = c.find(":");
  127. if (p==-1)
  128. continue;
  129. String prop = c.substr(0,p).strip_edges();
  130. String value = c.substr(p+1,c.length()).strip_edges();
  131. if (prop=="X-Language") {
  132. translation->set_locale(value);
  133. }
  134. }
  135. return translation;
  136. }
  137. void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const{
  138. p_extensions->push_back("po");
  139. //p_extensions->push_back("mo"); //mo in the future...
  140. }
  141. bool TranslationLoaderPO::handles_type(const String& p_type) const{
  142. return (p_type=="Translation");
  143. }
  144. String TranslationLoaderPO::get_resource_type(const String &p_path) const {
  145. if (p_path.extension().to_lower()=="po")
  146. return "Translation";
  147. return "";
  148. }
  149. TranslationLoaderPO::TranslationLoaderPO()
  150. {
  151. }