Source: mixins/sl-get-translation.js

  1. import Ember from 'ember';
  2. /**
  3. * @module
  4. * @augments ember/Mixin
  5. */
  6. export default Ember.Mixin.create({
  7. // -------------------------------------------------------------------------
  8. // Dependencies
  9. // -------------------------------------------------------------------------
  10. // Attributes
  11. // -------------------------------------------------------------------------
  12. // Actions
  13. // -------------------------------------------------------------------------
  14. // Events
  15. // -------------------------------------------------------------------------
  16. // Properties
  17. /**
  18. * Translation Service, used to convert content
  19. *
  20. * @type {Ember.Service}
  21. */
  22. translateService: Ember.inject.service( 'translate' ),
  23. // -------------------------------------------------------------------------
  24. // Observers
  25. // -------------------------------------------------------------------------
  26. // Methods
  27. /**
  28. * Based on value of key, retrieve translation or usual get() value
  29. *
  30. * @function
  31. * @param {String} key property to retrieve
  32. * @returns {String}
  33. */
  34. get( key ) {
  35. let matches = key.match( /translate\.(.*)/ );
  36. return matches ?
  37. this.translate( matches[1] ) :
  38. this._super( key );
  39. },
  40. /**
  41. * Retrieve translated key without support for token replacement or pluralization
  42. *
  43. * @function
  44. * @param {String} key key to translate
  45. * @returns {String}
  46. */
  47. translate( key ) {
  48. return this.get( 'translateService' ).getKeyValue( key );
  49. }
  50. });