general.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Created by jan on 11/03/2017.
  3. */
  4. 'use strict';
  5. console.log(`\n %c yunshop v1 %c https://www.yunz`+`shop.com \n\n`,"color: #fadfa3; background: #030307; padding:5px 0;","background: #fadfa3; padding:5px 0;");
  6. $.locales = {};
  7. $.currentLocale = {};
  8. /**
  9. * Check if given value is empty.
  10. *
  11. * @param {any} obj
  12. * @return {Boolean}
  13. */
  14. function isEmpty(obj) {
  15. // null and undefined are "empty"
  16. if (obj == null) return true;
  17. // Assume if it has a length property with a non-zero value
  18. // that that property is correct.
  19. if (obj.length > 0) return false;
  20. if (obj.length === 0) return true;
  21. // If it isn't an object at this point
  22. // it is empty, but it can't be anything *but* empty
  23. // Is it empty? Depends on your application.
  24. if (typeof obj !== "object") return true;
  25. // Otherwise, does it have any properties of its own?
  26. // Note that this doesn't handle
  27. // toString and valueOf enumeration bugs in IE < 9
  28. for (var key in obj) {
  29. if (hasOwnProperty.call(obj, key)) return false;
  30. }
  31. return true;
  32. }
  33. /**
  34. * Load current selected language.
  35. *
  36. * @return void
  37. */
  38. function loadLocales() {
  39. for (lang in $.locales) {
  40. if (!isEmpty($.locales[lang])) {
  41. $.currentLocale = $.locales[lang] || {};
  42. }
  43. }
  44. }
  45. /**
  46. * Translate according to given key.
  47. *
  48. * @param {string} key
  49. * @param {dict} parameters
  50. * @return {string}
  51. */
  52. function trans(key, parameters = {}) {
  53. if (isEmpty($.currentLocale)) {
  54. loadLocales();
  55. }
  56. let segments = key.split('.');
  57. let temp = $.currentLocale || {};
  58. for (let i in segments) {
  59. if (isEmpty(temp[segments[i]])) {
  60. return key;
  61. } else {
  62. temp = temp[segments[i]];
  63. }
  64. }
  65. for (let i in parameters) {
  66. if (!isEmpty(parameters[i])) {
  67. temp = temp.replace(':'+i, parameters[i]);
  68. }
  69. }
  70. return temp;
  71. }