languages.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*globals vsprintf*/
  2. /**
  3. * Provides language-related functionality
  4. */
  5. elgg.provide('elgg.config.translations');
  6. // default language - required by unit tests
  7. elgg.config.language = 'en';
  8. /**
  9. * Analagous to the php version. Merges translations for a
  10. * given language into the current translations map.
  11. */
  12. elgg.add_translation = function(lang, translations) {
  13. elgg.provide('elgg.config.translations.' + lang);
  14. elgg.extend(elgg.config.translations[lang], translations);
  15. };
  16. /**
  17. * Get the current language
  18. * @return {String}
  19. */
  20. elgg.get_language = function() {
  21. var user = elgg.get_logged_in_user_entity();
  22. if (user && user.language) {
  23. return user.language;
  24. }
  25. return elgg.config.language;
  26. };
  27. /**
  28. * Translates a string
  29. *
  30. * @param {String} key The string to translate
  31. * @param {Array} argv vsprintf support
  32. * @param {String} language The language to display it in
  33. *
  34. * @return {String} The translation
  35. */
  36. elgg.echo = function(key, argv, language) {
  37. //elgg.echo('str', 'en')
  38. if (elgg.isString(argv)) {
  39. language = argv;
  40. argv = [];
  41. }
  42. //elgg.echo('str', [...], 'en')
  43. var translations = elgg.config.translations,
  44. dlang = elgg.get_language(),
  45. map;
  46. language = language || dlang;
  47. argv = argv || [];
  48. map = translations[language] || translations[dlang];
  49. if (map && map[key]) {
  50. return vsprintf(map[key], argv);
  51. }
  52. return key;
  53. };