(say, target, language, fallback)
| 653 | // Example usage: |
| 654 | // `courseName = utils.i18n(course.attributes, 'name')` |
| 655 | var i18n = function (say, target, language, fallback) { |
| 656 | let generalName |
| 657 | if (language == null) { language = me.get('preferredLanguage', true) } |
| 658 | if (fallback == null) { fallback = 'en' } |
| 659 | let generalResult = null |
| 660 | let fallBackResult = null |
| 661 | let fallForwardResult = null // If a general language isn't available, the first specific one will do. |
| 662 | let fallSidewaysResult = null // If a specific language isn't available, its sibling specific language will do. |
| 663 | const matches = (/\w+/gi).exec(language) |
| 664 | if (matches) { generalName = matches[0] } |
| 665 | |
| 666 | // Lets us safely attempt to translate undefined objects |
| 667 | if (!(say != null ? say.i18n : undefined)) { return removeAI(say != null ? say[target] : undefined) } |
| 668 | |
| 669 | for (const localeName in say.i18n) { |
| 670 | var result |
| 671 | const locale = say.i18n[localeName] |
| 672 | if (localeName === '-') { continue } |
| 673 | if (target in locale && locale[target]) { |
| 674 | result = locale[target] |
| 675 | } else { continue } |
| 676 | if (localeName === language) { return removeAI(result) } |
| 677 | if (localeName === generalName) { generalResult = result } |
| 678 | if (localeName === fallback) { fallBackResult = result } |
| 679 | if ((localeName.indexOf(language) === 0) && (fallForwardResult == null)) { fallForwardResult = result } |
| 680 | if ((localeName.indexOf(generalName) === 0) && (fallSidewaysResult == null)) { fallSidewaysResult = result } |
| 681 | } |
| 682 | |
| 683 | if (generalResult != null) { return removeAI(generalResult) } |
| 684 | if (fallForwardResult != null) { return removeAI(fallForwardResult) } |
| 685 | if (fallSidewaysResult != null) { return removeAI(fallSidewaysResult) } |
| 686 | if (fallBackResult != null) { return removeAI(fallBackResult) } |
| 687 | if (target in say) { return removeAI(say[target]) } |
| 688 | return null // if we call i18n for a unexisting key |
| 689 | } |
| 690 | |
| 691 | const getByPath = function (target, path) { |
| 692 | if (!target) { throw new Error('Expected an object to match a query against, instead got null') } |
no test coverage detected