Skip to main content
InterviewHubPRO
Section Overview
Q240

Question 240 of 283

Part 9: NgModules, Providers, NgZone & Injector Hierarchies (213 - 255)

85%
Angular Master (283 Qs)•Part 9: NgModules, Providers, NgZone & Injector Hierarchies (213 - 255)

How do you get current direction for locales?

mid LevelCode ExampleDependency Injection

In Angular 9.1, the API method getLocaleDirection can be used to get the current direction in your app. This method is useful to support Right to Left locales for your Internationalization based applications.

js
import { getLocaleDirection, registerLocaleData } from '@angular/common';
import { LOCALE_ID } from '@angular/core';
import localeAr from '@angular/common/locales/ar';

...

constructor(@Inject(LOCALE_ID) locale) {

  const directionForLocale = getLocaleDirection(locale); // Returns 'rtl' or 'ltr' based on the current locale
  registerLocaleData(localeAr, 'ar-ae');
  const direction = getLocaleDirection('ar-ae'); // Returns 'rtl'

  // Current direction is used to provide conditional logic here
}