这可能很困难,尤其是在使用 aot 时。它通常需要您进行不同的构建。我扩展了数据管道并使用浏览器区域设置。
日期管:
@Pipe({name: 'datepipe', pure: true})
export class MyDatePipe extends DatePipe implements PipeTransform {
constructor(private win: WindowRef) {
super(win.ln);
}
transform(value: any, pattern?: string): string | null {
return super.transform(value, pattern);
}
}
Run Code Online (Sandbox Code Playgroud)
窗户:
function _window(): any {
// return the global native browser window object
return window;
}
@Injectable()
export class WindowRef {
get nativeWindow(): any {
return _window();
}
public ln = 'en';
constructor() {
try {
if (!isNullOrUndefined(this.nativeWindow.navigator.language) && this.nativeWindow.navigator.language !== '') {
this.ln = this.nativeWindow.navigator.language;
}
}finally {}
}
}
Run Code Online (Sandbox Code Playgroud)