Skip to content

Instantly share code, notes, and snippets.

@cwayfinder
Last active August 10, 2018 10:12
Show Gist options
  • Save cwayfinder/69b23453cfafa51908988b6d47ef8681 to your computer and use it in GitHub Desktop.
Save cwayfinder/69b23453cfafa51908988b6d47ef8681 to your computer and use it in GitHub Desktop.

Current approach with ngx-translate

Template:

<p>
  <span class="departure">{{ 'extras.available.LCO.departure' | translate: {time: departureTime} }}</span><br>
  {{ 'extras.available.LCO.proposal' | translate: {time: optionTime} }}<br>
</p>

<p>{{ 'extras.available.LCO.notification' | translate }}</p>

Approach for independent components

Template:

<p>
  <span class="departure">{{ intl.departureText(departureTime) }}</span><br>
  {{ intl.proposalText(optionTime) }}<br>
</p>

<p>{{ intl.notification }}</p>

Component:

@Component({
  selector: 'late-checkout-info',
  templateUrl: './late-checkout-info.component.html',
  styleUrls: ['./late-checkout-info.component.scss'],
})
export class LateCheckoutInfoComponent {

  @Input() departureTime: string;
  @Input() optionTime: string;

  constructor(public intl: LateCheckoutIntl) { }
}

Class providing localised labels:

@Injectable({ providedIn: 'root' })
export class LateCheckoutIntl {

  departureText(time: string): string {
    return `Your flight departs at ${time}`;
  }

  proposalText(time: string): string {
    return `Why not relax with a ${time} late checkout?`;
  }

  notification = 'Please note that you may be moved to a different room.';
}

How to override localisable texts:

providers: [
  { provide: LateCheckoutIntl, useClass: LateCheckoutIntlNecnlNl },
],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment