Skip to content
Related Articles

Related Articles

Angular10 getLocaleDirection() Function

Improve Article
Save Article
  • Last Updated : 28 Apr, 2021
Improve Article
Save Article

In this article, we are going to see what is getLocaleDirection in Angular 10 and how to use it.

The getLocaleDirection is used to get the writing direction for the given locale.

Syntax:

getLocaleDirection(locale: string): 'ltr' | 'rtl'

NgModule: Module used by getLocaleDirection is:

  • CommonModule

Approach: 

  • Create the angular app to be used
  • In app.module.ts import LOCALE_ID because we need locale to be imported for using get getLocaleDirection.
import { LOCALE_ID, NgModule } from '@angular/core';
  • In app.component.ts import getLocaleDirection and LOCALE_ID
  • inject LOCALE_ID as a public variable.
  • In app.component.html show the local variable using string interpolation
  • serve the angular app using ng serve to see the output.

Parameters:

  • locale: A string containing locale code with rules.

Return value:

  • string : string containing the writing direction.

Example 1:

app.module.ts




import { LOCALE_ID, NgModule } 
        from '@angular/core';
import { BrowserModule } 
        from '@angular/platform-browser';
  
import { AppRoutingModule } 
        from './app-routing.module';
import { AppComponent } 
        from './app.component';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [
      { provide: LOCALE_ID, useValue: 'en-GB' },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts




import {FormStyle,
        getLocaleDirection, 
        TranslationWidth } 
        from '@angular/common';
  
import {Component, 
        Inject,OnInit, 
        LOCALE_ID } 
        from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    dir = getLocaleDirection(this.locale);
    constructor(
        @Inject(LOCALE_ID) public locale: string,){}
      }

app.component.html




<h1>
   GeeksforGeeks
</h1>
<p>Locale Direction is : {{dir}}</p>

Output:

Example 2:

app.module.ts




import { LOCALE_ID, NgModule } 
        from '@angular/core';
import { BrowserModule } 
        from '@angular/platform-browser';
  
import { AppRoutingModule } 
        from './app-routing.module';
import { AppComponent } 
        from './app.component';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [
      { provide: LOCALE_ID, useValue: 'en-GB' },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts




import {FormStyle,
        getLocaleDirection, 
        TranslationWidth } 
        from '@angular/common';
  
import {Component, 
        Inject,
        OnInit, 
        LOCALE_ID } 
        from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    dir = getLocaleDirection(this.locale);
    a=''
    constructor(
        @Inject(LOCALE_ID) public locale: string,){
            if(this.dir=='ltr'){
                this.a='left to right';
            }
        }
      }

app.component.html




<h1>
   GeeksforGeeks
</h1>
<p><b>{{ a }}</b> is the direction</p>

Output:

Reference: https://angular.io/api/common/getLocaleDirection


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!