मुझे अपने कोणीय अनुप्रयोग में कोड में अनुवादित तारों का उपयोग करने में सक्षम होना चाहिए, लेकिन i18n उपकरण अभी तक कार्य तक नहीं हैं, मैंने थोड़ा सा हैकी संस्करण लागू किया है जो चरणबद्ध करने के इरादे से कोणीय की मौजूदा i18n क्षमताओं का लाभ उठाता है देशी कोणीय के लिए मेरा समाधान बाहर करें क्योंकि इसकी आई 18 एन क्षमताओं को मेरी जरूरतों को पूरा करता है (5.x release होना चाहिए, उंगलियों को पार किया जाना चाहिए)।क्या किसी तत्व की टेक्स्ट सामग्री डीओएम से पढ़ने के बिना कोणीय में पढ़ी जा सकती है?
@Directive({
selector: '[myAppTranslation]'
})
export class TranslationDirective implements AfterViewChecked, OnChanges {
/**
* dependencies takes an array of the values needed to calculate the output translation
* we use this for change detection, to minimize the DOM interaction to only when it is necessary
*/
@Input() dependencies: any[];
isDirty = true;
@Input() messageKey: string;
message: string;
@Output() messageUpdated = new EventEmitter<TranslationEvent>();
constructor(public el: ElementRef) {}
/**
* sets the translated message and triggers the TranslationEvent
*/
setMessage() {
const message = (this.el.nativeElement.textContent || '').trim();
const oldMessage = (this.message || '');
if (oldMessage !== message) {
this.message = message;
this.isDirty = false;
this.triggerTranslationEvent();
}
}
ngOnChanges() {
this.isDirty = true;
}
ngAfterViewChecked() {
if (this.isDirty) {
this.setMessage();
}
}
/**
* triggers the messageUpdated EventEmitter with the TranslationEvent
*/
triggerTranslationEvent() {
// need to delay a tick so Angular doesn't throw an ExpressionChangedAfterItHasBeenCheckedError
setTimeout(() => {
const event = new TranslationEvent(this.messageKey, this.message);
this.messageUpdated.emit(event);
});
}
}
export class TranslationEvent {
constructor(public messageKey: string, public message: string) {}
}
इस तरह इस्तेमाल किया जाता है जो:
<span
myAppTranslation
i18n
[dependencies]="[today]"
[messageKey]="todaysDateKey"
(messageUpdated)="setTodaysDateTranslation($event)"
>
Today is {{today | date:'short'}}
</span>
के बाद से तार होने की
मूल रूप से, मैं एक TranslationDirective
जो DOM से यह लिखा होता है, और जब पाठ परिवर्तन की घटनाओं का उत्सर्जन करता है है अनुवादित सभी टेम्पलेट्स में रहते हैं, कोणीय के xi18n उपकरण उन्हें ठीक से पढ़ते हैं, और कोणीय संकलक उन्हें अनुवादित तारों के साथ बदल देगा।
यह कार्यात्मक है, लेकिन यह बहुत अच्छा नहीं है। मुझे संदेह है कि एक समय बग सिर्फ मुझे काटने का इंतजार कर रहा है जो अभी तक प्रकट नहीं हुआ है। एक अक्षम और धीमी लेखन-टू-डोम-रीड-टू-डीओएम चक्र है जो खत्म करने के लिए वास्तव में अच्छा होगा।
यदि मैं इसे से बच सकता हूं तो मैं डोम को छोड़कर समस्याओं के एक स्रोत को खत्म करने में सक्षम होना चाहता हूं। क्या कोणीय एक तत्व की सामग्री की एक स्मृति-स्मृति प्रतिलिपि रखता है जो डोम के माध्यम से बिना पहुंच के सुलभ है? यदि यह संभव है, तो क्या मैं पूरी तरह से डीओएम में अनुवाद तत्व लिखने से बच सकता हूं?
ngModel का उपयोग करने के बारे में क्या? और कृपया उस प्रारूप में प्लंकर लिंक –