简体   繁体   中英

Vaddin RTL support, get direction

I need to add RTL languages support to my application. I know how to set RTL:

UI.getCurrent().setDirection(Direction.RIGHT_TO_LEFT)

but I also need to programmatically get this value in order to do some changes in the code.

How to get current Direction value in the view?

Smells like a missing API, but for now, due to the async nature of executeJs , you might consider the following:

public void fetchPageDirection(SerializableConsumer<Direction> callback) {
    UI.getCurrent().getPage()
            .executeJs("return document.dir")
            .then(String.class, dir -> {
                Direction direction = getDirectionByClientName(dir);
                callback.accept(direction);
            });
}

private Direction getDirectionByClientName(String directionClientName) {
    return Arrays.stream(Direction.values())
            .filter(direction -> direction.getClientName().equals(directionClientName))
            .findFirst().orElse(Direction.LEFT_TO_RIGHT); // Default to LTR
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM