locale.service.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @file Provides a service to handle languages and localizations.
  3. * @author Inigo Valentin
  4. * @since 4.0.0
  5. */
  6. /**
  7. * Handles localization of elements.
  8. */
  9. class LocaleService {
  10. /**
  11. * Database connection.
  12. */
  13. #db;
  14. /**
  15. * List of available languages.
  16. *
  17. * The one at index 0 is the default language.
  18. */
  19. #availableLanguages = Array();
  20. /**
  21. * Constructor.
  22. *
  23. * @param db Database connection.
  24. * @constructor
  25. */
  26. constructor(db){
  27. this.#db = db;
  28. this.#init();
  29. }
  30. /**
  31. * Initializes the service.
  32. *
  33. * Called automatically from the constructor.
  34. */
  35. async #init(){
  36. var result = await this.#db.sequelize.query('SELECT code FROM langs WHERE active = 1 ORDER by PRIORITY ASC ', { type: this.#db.sequelize.QueryTypes.SELECT })
  37. for (const r of result) this.#availableLanguages.push(r.code.toLowerCase().substring(0, 2));
  38. }
  39. /**
  40. * Selects the language to use.
  41. *
  42. * If the language is not in the request, using the GET parameter "lang", the default one set
  43. * in the database will be used.
  44. *
  45. * @param req The request received by the server.
  46. * @return Two letter language code to use.
  47. */
  48. selectLanguage(req){
  49. if (!req.query.lang) return this.#availableLanguages[0];
  50. var requestedLang = req.query.lang.toLowerCase().substring(0, 2)
  51. if (this.#availableLanguages.indexOf(requestedLang) != -1) return requestedLang;
  52. return this.#availableLanguages[0];
  53. }
  54. /**
  55. * Decodes a text from the database.
  56. *
  57. * @param result Result of a query to the table 'texts'. Must have at least the columns 'text' and 'file'.
  58. * @return The text.
  59. */
  60. async #decodeText(key, lang){
  61. var result = await this.#db.sequelize.query('SELECT text, file FROM texts WHERE lang = ? AND id = ?', { replacements: [lang, key], type: this.#db.sequelize.QueryTypes.SELECT })
  62. for (const r of result) {
  63. if (r.text) return "" + r.text;
  64. if (r.file) return "" + r.file; // TODO: Return file CONTENTS
  65. }
  66. return "";
  67. }
  68. /**
  69. * Localizes a project.
  70. *
  71. * Localizes all localizable items in a project, using the language provided in the request or
  72. * the default one.
  73. *
  74. * @param data The project object to localize.
  75. * @param req The request received by the server.
  76. * @return The project object, with all of the fields localized.
  77. */
  78. async localizeProject(data, req){
  79. var lang = this.selectLanguage(req);
  80. data.dataValues.title = await this.#decodeText(data.dataValues.title, lang);
  81. data.dataValues.header = await this.#decodeText(data.dataValues.header, lang);
  82. data.dataValues.text = await this.#decodeText(data.dataValues.text, lang);
  83. data.dataValues.comment = await this.#decodeText(data.dataValues.comment, lang);
  84. if (data.dataValues.type){
  85. data.dataValues.type.title = await this.#decodeText(data.dataValues.type.title, lang);
  86. data.dataValues.type.summary = await this.#decodeText(data.dataValues.type.summary, lang);
  87. }
  88. if (data.dataValues.license){
  89. data.dataValues.license.summary = await this.#decodeText(data.dataValues.license.summary, lang);
  90. data.dataValues.license.legal = await this.#decodeText(data.dataValues.license.legal, lang);
  91. }
  92. for (var i = 0; i < data.dataValues.tags.length; i ++)
  93. data.dataValues.tags[i].tag = await this.#decodeText(data.dataValues.tags[i].tag, lang);
  94. for (var i = 0; i < data.dataValues["project-urls"].length; i ++){
  95. data.dataValues["project-urls"][i].dataValues.type.dataValues.title = await this.#decodeText(data.dataValues["project-urls"][i].dataValues.type.dataValues.title, lang);
  96. data.dataValues["project-urls"][i].dataValues.type.dataValues.summary = await this.#decodeText(data.dataValues["project-urls"][i].dataValues.type.dataValues.summary, lang);
  97. }
  98. for (var i = 0; i < data.dataValues["project-images"].length; i ++)
  99. data.dataValues["project-images"][i].dataValues.alt = await this.#decodeText(data.dataValues["project-images"][i].dataValues.alt, lang);
  100. //console.log(data.dataValues.projectUrls)
  101. return data;
  102. }
  103. /**
  104. * Localizes a list of projects.
  105. *
  106. * Localizes all localizable items in all projects, using the language provided in the request or
  107. * the default one.
  108. *
  109. * @param data The project object list to localize.
  110. * @param req The request received by the server.
  111. * @return The project list, with all of their fields localized.
  112. */
  113. async localizeProjects(data, req){
  114. for (var d of data) d = await this.localizeProject(d, req);
  115. return data;
  116. }
  117. /**
  118. * Localizes a license.
  119. *
  120. * Localizes all localizable items in a license, using the language provided in the request or
  121. * the default one.
  122. *
  123. * @param data The license object to localize.
  124. * @param req The request received by the server.
  125. * @return The license object, with all of the fields localized.
  126. */
  127. async localizeLicense(data, req){
  128. var lang = this.selectLanguage(req);
  129. data.dataValues.summary = await this.#decodeText(data.dataValues.summary, lang);
  130. data.dataValues.legal = await this.#decodeText(data.dataValues.legal, lang);
  131. return data;
  132. }
  133. /**
  134. * Localizes a list of projects licenses.
  135. *
  136. * Localizes all localizable items in all licenses, using the language provided in the request or
  137. * the default one.
  138. *
  139. * @param data The license object list to localize.
  140. * @param req The request received by the server.
  141. * @return The license list, with all of their fields localized.
  142. */
  143. async localizeLicenses(data, req){
  144. for (var d of data) d = await this.localizeLicense(d, req);
  145. return data;
  146. }
  147. /**
  148. * Localizes a project type.
  149. *
  150. * Localizes all localizable items in a project type, using the language provided in the request or
  151. * the default one.
  152. *
  153. * @param data The project type object to localize.
  154. * @param req The request received by the server.
  155. * @return The project type object, with all of the fields localized.
  156. */
  157. async localizeProjectType(data, req){
  158. var lang = this.selectLanguage(req);
  159. data.dataValues.title = await this.#decodeText(data.dataValues.title, lang);
  160. data.dataValues.summary = await this.#decodeText(data.dataValues.summary, lang);
  161. return data;
  162. }
  163. /**
  164. * Localizes a list of projects project types.
  165. *
  166. * Localizes all localizable items in all project types, using the language provided in the request or
  167. * the default one.
  168. *
  169. * @param data The project type object list to localize.
  170. * @param req The request received by the server.
  171. * @return The project type list, with all of their fields localized.
  172. */
  173. async localizeProjectTypes(data, req){
  174. for (var d of data) d = await this.localizeProjectType(d, req);
  175. return data;
  176. }
  177. }
  178. module.exports = LocaleService;