-
-
Save charlespockert/4460ebe5d8e1fd274767f72005ea5cba to your computer and use it in GitHub Desktop.
Aurelia + i18n plugin issue with binding behaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<require from="tbind.js"></require> | |
<h1>${message}</h1> | |
<h2>${prop}</h2> | |
<div>${'val1' & t2}</div> | |
<div>${'val1' & t2}</div> | |
<div>${'val1' & t2}</div> | |
<hr/> | |
<div> | |
<compose containerless view.bind="'custom-view.html'"></compose> | |
</div> | |
<hr/> | |
<button click.trigger="changeLocale('de')">de</button> | |
<button click.trigger="changeLocale('en')">en</button> | |
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {BindingSignaler} from 'aurelia-templating-resources'; | |
import {I18N} from 'aurelia-i18n'; | |
import {inject} from 'aurelia-framework'; | |
@inject(I18N, BindingSignaler) | |
export class App { | |
message = 'Hello World'; | |
prop = "test"; | |
constructor(i18n, bindingSignaler) { | |
this.signaler = bindingSignaler; | |
this.i18n = i18n; | |
} | |
changeLocale(locale) { | |
this.i18n.setLocale(locale); | |
this.signaler.signal('trans'); | |
} | |
getLabel(propName) { | |
return propName; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
Custom view | |
<p> | |
${'val1' | t & signal:'trans'} | |
</p> | |
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Aurelia</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<body aurelia-app="main"> | |
<h1>Loading...</h1> | |
<script src="https://cdn.rawgit.com/valichek/aurelia-bundle/i18n-0.5.2v0.0.4/jspm_packages/system.js"></script> | |
<script src="https://cdn.rawgit.com/valichek/aurelia-bundle/i18n-0.5.2v0.0.4/config.js"></script> | |
<script> | |
System.import('aurelia-bootstrapper'); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"val" : "DE" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"val1" : "DE-tr1", | |
"val1test" : "DE-tr1-test", | |
"DE-tr1" : "wtf" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"val" : "EN" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"val1" : "EN-tr1", | |
"val1test" : "EN-tr1-test" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Backend from 'i18next-xhr-backend'; | |
export function configure(aurelia) { | |
aurelia.use | |
.standardConfiguration() | |
.developmentLogging() | |
.plugin('aurelia-i18n', (instance) => { | |
// register backend plugin | |
instance.i18next.use(Backend); | |
// adapt options to your needs (see http://i18next.com/docs/options/) | |
return instance.setup({ | |
backend: { // <-- configure backend settings | |
loadPath: 'locale-{{lng}}-{{ns}}.json' // <-- XHR settings for where to get the files from | |
}, | |
lng: 'de', | |
attributes: ['t', 'i18n'], | |
fallbackLng: 'en', | |
debug: true, | |
//compatibilityJSON: 'v1', | |
ns: ['translation', 'custom'] | |
}); | |
}); | |
aurelia.start().then(a => a.setRoot()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {BindingSignaler, SignalBindingBehavior} from 'aurelia-templating-resources'; | |
import {ValueConverter} from 'aurelia-binding'; | |
export class T2BindingBehavior { | |
static inject = [SignalBindingBehavior]; | |
constructor(signalBindingBehavior) { | |
this.signalBindingBehavior = signalBindingBehavior; | |
} | |
bind(binding, source) { | |
// bind the signal behavior | |
this.signalBindingBehavior.bind(binding, source, 'aurelia-translation-signal'); | |
// rewrite the expression to use the TValueConverter. | |
// pass through any args to the binding behavior to the TValueConverter | |
let sourceExpression = binding.sourceExpression; | |
let expression = sourceExpression.expression; | |
// Bump the chain back 1 hop - temporary fix... | |
if(expression.allArgs) | |
expression = expression.allArgs[0]; | |
sourceExpression.expression = new ValueConverter( | |
expression, | |
't', | |
sourceExpression.args, | |
[expression, ...sourceExpression.args]); | |
} | |
unbind(binding, source) { | |
// undo the expression rewrite | |
binding.sourceExpression.expression = binding.sourceExpression.expression.expression; | |
// unbind the signal behavior | |
this.signalBindingBehavior.unbind(binding, source); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment