Generating i18n links during server startup

17 views
Skip to first unread message

samuli....@gmail.com

unread,
May 20, 2022, 9:36:37 AM5/20/22
to user...@magnolia-cms.com

Hi,

 

I’m running Magnolia 6.2 w/ translated urls (magnolia-urltranslation@5.6) and would like to generate some links during server startup. LinkUtil.createLink(node) would do the job but the problem is that it uses MgnlContext.getAggregationState().getLocale() that needs WebContext, which isn’t available during server startup. I can use urlTranslator directly but there is also URI2repository-mapping involved which seems to be the part that actually requires the aggregation state.  Does anybody know an alternative way to make this happen or is this just not possible?

 

BR, Samuli

Roman Kovařík

unread,
May 24, 2022, 6:46:54 AM5/24/22
to Magnolia User Mailing List, samuli....@gmail.com
Hi Samuli,
linking uses a lot of request specific information like site, contextPath etc...
It could be possible with these workarounds but it's a fragile logic:

public class MyModule {

    @Inject
    MyModule(@Named(SystemEventBus.NAME) EventBus systemEventBus, SiteManager siteManager, I18nContentSupport i18nContentSupport) {
        systemEventBus.addHandler(ModulesStartedEvent.class, event -> {
            Context originalCtx = MgnlContext.getInstance();
            try {
                WebContext webContext = new WebContextImpl() {
                    @Override
                    protected AggregationState newAggregationState() {
                        ExtendedAggregationState extendedAggregationState = new ExtendedAggregationState();
                        extendedAggregationState.setRepository("website");
                        extendedAggregationState.setSite(siteManager.getSites().stream().findFirst().orElse(null));
                        return extendedAggregationState;
                    }

                    @Override
                    public String getContextPath() {
                        return "/";
                    }
                };
                webContext.init(null, null, null);
                webContext.setLocale(Locale.GERMAN);
                MgnlContext.setInstance(webContext);
                i18nContentSupport.setLocale(Locale.GERMAN);
                String link = LinkUtil.createLink(originalCtx.getJCRSession("website").getNode("/travel/about"));
            } catch (RepositoryException e) {
                throw new RuntimeRepositoryException(e);
            } finally {
                MgnlContext.setInstance(originalCtx);
            }
        });
    }

Hope that helps.
Roman

samuli....@gmail.com

unread,
May 25, 2022, 12:44:50 AM5/25/22
to Roman Kovařík, Magnolia User Mailing List

Thanks for the example. Looks pretty intimidating and as you said fragile. I was thinking of using it as part of a virtual uri mapping to “autogenerate” i18n paths in fromUri regexses based on default path but I don’t know if it’s such a great idea in the first place even if generating the paths would be easier. Do you happen to know if there is any other options I could consider using virtualUris and i18n urls? At this point I think I will leave it up to the configuration/configurer to handle i18n urls correctly.

 

BR, Samuli

Reply all
Reply to author
Forward
0 new messages