Skip to content

Commit

Permalink
feat: add fallback locales to ensure we display something
Browse files Browse the repository at this point in the history
This will help reduce the problem for niche players that sometimes
get overlooked by the majority when adding content.
  • Loading branch information
fkm-adfinis committed Mar 17, 2021
1 parent 372526b commit 6237bc3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
22 changes: 21 additions & 1 deletion addon/-private/decorators/localized-attr.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getOwner } from '@ember/application';
import { isEmpty } from '@ember/utils';
import { attr } from "@ember-data/model";

export default function (...args) {
Expand All @@ -11,7 +13,25 @@ export default function (...args) {
attrComputed.get = function () {
const value = getter.call(this);

return value && value[this.getFieldLocale()];
if (!value) {
return;
}

const {
localizedModel: { allowAnyFallback = false, fallbacks = [] } = {}
} = getOwner(this).resolveRegistration('config:environment');

if(value[this.getFieldLocale()] || (!allowAnyFallback && !fallbacks.length)){
return value[this.getFieldLocale()];
}

let key = fallbacks.find(key => !isEmpty(value[key]));

if (!key && allowAnyFallback) {
key = Object.keys(value).find(key => !isEmpty(value[key]));
}

return value[key]
};

attrComputed.set = function (value) {
Expand Down
11 changes: 10 additions & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
'use strict';

module.exports = function(/* environment, appConfig */) {
return { };
return {
localizedModel: {
// If this is true the decorator will get the value in any language
// if neither the preferred language nor the fallbacks yield anything.
allowAnyFallback: false,
// Set fallback languages in order they should be tried
// if the preferred language has no value.
fallbacks: [],
},
};
};

0 comments on commit 6237bc3

Please sign in to comment.