@hmcts/ccd-case-ui-toolkit 7.3.46 → 7.3.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6624,6 +6624,7 @@ class LabelSubstitutorDirective {
6624
6624
  contextFields = [];
6625
6625
  formGroup;
6626
6626
  elementsToSubstitute = ['label', 'hint_text'];
6627
+ initialLabel;
6627
6628
  initialHintText;
6628
6629
  languageSubscription;
6629
6630
  constructor(fieldsUtils, placeholderService, rpxTranslationPipe, rpxTranslationService) {
@@ -6633,9 +6634,10 @@ class LabelSubstitutorDirective {
6633
6634
  this.rpxTranslationService = rpxTranslationService;
6634
6635
  }
6635
6636
  ngOnInit() {
6637
+ this.initialLabel = this.caseField.label;
6636
6638
  this.initialHintText = this.caseField.hint_text;
6637
- this.caseField.originalLabel = this.caseField.label;
6638
6639
  this.noCacheProcessing();
6640
+ this.caseField.originalLabel = this.caseField.originalLabel || this.caseField.label;
6639
6641
  this.formGroup = this.formGroup || new FormGroup({});
6640
6642
  this.languageSubscription = this.rpxTranslationService.language$.pipe(skip(1)).subscribe(() => {
6641
6643
  this.onLanguageChange();
@@ -6658,23 +6660,10 @@ class LabelSubstitutorDirective {
6658
6660
  }
6659
6661
  }
6660
6662
  }
6661
- applySubstitutions() {
6663
+ applySubstitutions(isLanguageChange = false) {
6662
6664
  const fields = this.getReadOnlyAndFormFields();
6663
6665
  if (this.shouldSubstitute('label')) {
6664
- const oldLabel = this.caseField.label;
6665
- const substitutedLabel = this.resolvePlaceholders(fields, this.caseField.label);
6666
- if (oldLabel && oldLabel !== substitutedLabel) {
6667
- // we need to translate the uninterpolated data then substitute the values in translated string
6668
- this.caseField.originalLabel = substitutedLabel;
6669
- const translated = this.rpxTranslationPipe.transform(oldLabel);
6670
- const transSubstitutedLabel = this.resolvePlaceholders(fields, translated);
6671
- this.caseField.label = transSubstitutedLabel;
6672
- this.caseField.isTranslated = this.rpxTranslationService.language === 'cy' && translated !== oldLabel;
6673
- }
6674
- else {
6675
- this.caseField.label = substitutedLabel;
6676
- this.caseField.isTranslated = false;
6677
- }
6666
+ this.applyLabelSubstitution(fields, isLanguageChange);
6678
6667
  }
6679
6668
  if (this.shouldSubstitute('hint_text')) {
6680
6669
  this.caseField.hint_text = this.resolvePlaceholders(fields, this.caseField.hint_text);
@@ -6683,14 +6672,83 @@ class LabelSubstitutorDirective {
6683
6672
  this.caseField.value = this.resolvePlaceholders(fields, this.caseField.value);
6684
6673
  }
6685
6674
  }
6675
+ applyLabelSubstitution(fields, isLanguageChange) {
6676
+ const currentLabel = this.caseField.label;
6677
+ // `originalLabel` stores the label exactly as it came from the server, before any
6678
+ // placeholder values were inserted. That gives us a clean starting point when the user
6679
+ // changes language or returns to the page later.
6680
+ const originalLabel = this.caseField.originalLabel || currentLabel;
6681
+ const substitutedCurrentLabel = this.resolvePlaceholders(fields, currentLabel);
6682
+ const substitutedOriginalLabel = originalLabel === currentLabel
6683
+ ? substitutedCurrentLabel
6684
+ : this.resolvePlaceholders(fields, originalLabel);
6685
+ const substitutedLabel = substitutedCurrentLabel || substitutedOriginalLabel;
6686
+ const hasAnyLabelSubstitution = (currentLabel && currentLabel !== substitutedCurrentLabel)
6687
+ || (originalLabel && originalLabel !== substitutedOriginalLabel);
6688
+ if (!hasAnyLabelSubstitution) {
6689
+ // No placeholders were resolved, so keep the current label and allow the render layer
6690
+ // to translate it normally if needed.
6691
+ this.setLabelState(substitutedLabel);
6692
+ return;
6693
+ }
6694
+ // Preserve the original template the first time we successfully interpolate it.
6695
+ this.caseField.originalLabel = this.caseField.originalLabel || originalLabel;
6696
+ this.applyTranslatedLabelState(fields, originalLabel, substitutedLabel, isLanguageChange);
6697
+ }
6698
+ applyTranslatedLabelState(fields, originalLabel, substitutedLabel, isLanguageChange) {
6699
+ // Some labels only translate correctly if we translate the template first and then
6700
+ // substitute the helper values into the translated sentence.
6701
+ const translatedTemplateLabel = this.resolvePlaceholders(fields, isLanguageChange ? this.translateLabelOnLanguageChange(originalLabel) : this.translateLabel(originalLabel));
6702
+ // Other labels only translate correctly if we first resolve the English phrase and let
6703
+ // the render layer translate that final resolved string.
6704
+ const translatedResolvedLabel = isLanguageChange
6705
+ ? this.translateLabelOnLanguageChange(substitutedLabel)
6706
+ : this.translateLabel(substitutedLabel);
6707
+ const languageIsWelsh = this.rpxTranslationService.language === 'cy';
6708
+ const hasResolvedWelshTranslation = languageIsWelsh
6709
+ && translatedResolvedLabel
6710
+ && translatedResolvedLabel !== substitutedLabel;
6711
+ const hasTemplateWelshTranslation = languageIsWelsh
6712
+ && translatedTemplateLabel
6713
+ && translatedTemplateLabel !== substitutedLabel;
6714
+ if (hasResolvedWelshTranslation) {
6715
+ // Keep the resolved English label and mark it as not yet translated so the field
6716
+ // template can run `rpxTranslate` on the full phrase at render time.
6717
+ this.setLabelState(substitutedLabel);
6718
+ return;
6719
+ }
6720
+ if (hasTemplateWelshTranslation) {
6721
+ // Use the template-translated result when translating the fully resolved label does
6722
+ // not improve the Welsh output.
6723
+ this.setLabelState(translatedTemplateLabel, true);
6724
+ return;
6725
+ }
6726
+ // English, untranslated Welsh, or labels whose translation is handled elsewhere.
6727
+ this.setLabelState(substitutedLabel);
6728
+ }
6729
+ translateLabel(label) {
6730
+ return this.rpxTranslationPipe.transform(label);
6731
+ }
6732
+ translateLabelOnLanguageChange(label) {
6733
+ return this.rpxTranslationService.language === 'en'
6734
+ ? label
6735
+ : this.translateLabel(label);
6736
+ }
6737
+ setLabelState(label, isTranslated = false) {
6738
+ this.caseField.label = label;
6739
+ this.caseField.isTranslated = isTranslated;
6740
+ }
6686
6741
  onLanguageChange() {
6687
- this.resetToInitialValues();
6688
- this.applySubstitutions();
6742
+ this.resetToInitialValues(true);
6743
+ this.applySubstitutions(true);
6689
6744
  }
6690
- resetToInitialValues() {
6691
- if (this.caseField?.originalLabel) {
6745
+ resetToInitialValues(isLanguageChange = false) {
6746
+ if (isLanguageChange && this.caseField?.originalLabel) {
6692
6747
  this.caseField.label = this.caseField.originalLabel;
6693
6748
  }
6749
+ if (!isLanguageChange && this.initialLabel) {
6750
+ this.caseField.label = this.initialLabel;
6751
+ }
6694
6752
  if (this.initialHintText) {
6695
6753
  this.caseField.hint_text = this.initialHintText;
6696
6754
  }