Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

internationalization - Render Angular component in a locale which is calculated dynamically

I would like to give user opportunity to render one component in a locale which is may be different to application locale.

Something like that (code below does not work, component is being rendered in a application locale)

@Component({
    selector: 'preview',
    templateUrl: './preview.html',
    styleUrls: ['./preview.scss'],
    providers: [
        {
            provide: LOCALE_ID,
            useFactory: () => window.location.href.indexOf('preview=true') >= 0 ? 'ja' : 'en'
        }
    ],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class PreviewComponent {

    constructor(@Inject(LOCALE_ID) private locale: any) {
        console.log(this.locale); // Here injected locale is correct (ja or en) but component is rendered in current application's locale.
    }

}


Alternatively, since my component is quite small, I'm fine to render it by myself in a proper locale if Angular provides a way to access localization bundles, i.e.

@Component({
    selector: 'preview',
    templateUrl: './preview.html',
    styleUrls: ['./preview.scss'],
    providers: [
        {
            provide: LOCALE_ID,
            useFactory: () => window.location.href.indexOf('preview=true') >= 0 ? 'ja' : 'en'
        }
    ],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class PreviewComponent {

    hello: string;
    
    constructor(@Inject(LOCALE_ID) private locale: any, 
        private angularTranslationService: angularTranslationService // IS THERE SUCH ONE????
) {
        this.hello = this.angularTranslationService.localize(locale, '@@my.i18n.custom.id.for.hello.string'); // IS THERE SUCH API???? 
    }

}

question from:https://stackoverflow.com/questions/65847472/render-angular-component-in-a-locale-which-is-calculated-dynamically

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...