Localization & URLs for Default Language

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Localization & URLs for Default Language

chiefsucker
Hello,

Tapestry supports putting the current locale into the URL path. I’m injecting PersistentLocale and switching between languages as advised in the documentation:

if (persistentLocale.get() == null) {
    persistentLocale.set(new Locale("de"));
} else if ("en".equalsIgnoreCase(persistentLocale.get().getLanguage())) {
    persistentLocale.set(new Locale("de"));
} else {
    persistentLocale.set(new Locale("en"));
}

The problem is that the default language should not have a prefixed URL path. When the user goes to https://example.com/ the default language is rendered and it is the desired behavior. When I switch the URL becomes https://example.com/de/ which is also OK. But now when I try to switch back to English (to the default language), the URL becomes https://example.com/en/ which I don’t want. Trying to set Locale.ROOT as the locale for the default language after switching from German doesn’t work, because "" (which Locale.ROOT effectively represents) isn’t configured in SymbolConstants.SUPPORTED_LOCALES.

The only thing I’ve seen are some older threads where PersistentLocale was copied, pasted & adjusted to allow null parameters. Is this workaround the only way to go or is there an official, Tapestry way to not have the locale in the URL path for the default language?

Best,
Rafael


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Localization & URLs for Default Language

Cezary Biernacki
You can always decorate PersistentLocale and change behaviour of get() by
returning null from it, when the original result is your default locale.

Cezary


On Fri, Sep 6, 2019 at 10:45 AM Rafael Bugajewski <[hidden email]>
wrote:

> Hello,
>
> Tapestry supports putting the current locale into the URL path. I’m
> injecting PersistentLocale and switching between languages as advised in
> the documentation:
>
> if (persistentLocale.get() == null) {
>     persistentLocale.set(new Locale("de"));
> } else if ("en".equalsIgnoreCase(persistentLocale.get().getLanguage())) {
>     persistentLocale.set(new Locale("de"));
> } else {
>     persistentLocale.set(new Locale("en"));
> }
>
> The problem is that the default language should not have a prefixed URL
> path. When the user goes to https://example.com/ the default language is
> rendered and it is the desired behavior. When I switch the URL becomes
> https://example.com/de/ which is also OK. But now when I try to switch
> back to English (to the default language), the URL becomes
> https://example.com/en/ which I don’t want. Trying to set Locale.ROOT as
> the locale for the default language after switching from German doesn’t
> work, because "" (which Locale.ROOT effectively represents) isn’t
> configured in SymbolConstants.SUPPORTED_LOCALES.
>
> The only thing I’ve seen are some older threads where PersistentLocale was
> copied, pasted & adjusted to allow null parameters. Is this workaround the
> only way to go or is there an official, Tapestry way to not have the locale
> in the URL path for the default language?
>
> Best,
> Rafael
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Localization & URLs for Default Language

chiefsucker
Hello Cezary,

that’s actually a great idea. I tried to add a service advisor, but it doesn’t seem to do what I want:

@Match("PersistentLocale")
public static void adviseNullForDefaultLanguage(MethodAdviceReceiver receiver) {
    MethodAdvice advice = new MethodAdvice() {
        public void advise(MethodInvocation invocation) {
            invocation.proceed();

            if (invocation.getReturnValue() != null && invocation.getReturnValue().equals("en")) {
                invocation.setReturnValue(null);
            }
        }
    };

    for (Method m : receiver.getInterface().getMethods()) {
        if (m.getName().equals("get"))
            receiver.adviseMethod(m, advice);
    }
}

The basic idea was that a method called "get" on the service called "PersistentLocale" returns null if the return value from the implementation is "en", but actually nothing happens.

Any ideas what I’m doing wrong?

Best,
Rafael


> On 2019-06-09, at 12:10 PM, Cezary Biernacki <[hidden email]> wrote:
>
> You can always decorate PersistentLocale and change behaviour of get() by
> returning null from it, when the original result is your default locale.
>
> Cezary
>
>
> On Fri, Sep 6, 2019 at 10:45 AM Rafael Bugajewski <[hidden email]>
> wrote:
>
>> Hello,
>>
>> Tapestry supports putting the current locale into the URL path. I’m
>> injecting PersistentLocale and switching between languages as advised in
>> the documentation:
>>
>> if (persistentLocale.get() == null) {
>>    persistentLocale.set(new Locale("de"));
>> } else if ("en".equalsIgnoreCase(persistentLocale.get().getLanguage())) {
>>    persistentLocale.set(new Locale("de"));
>> } else {
>>    persistentLocale.set(new Locale("en"));
>> }
>>
>> The problem is that the default language should not have a prefixed URL
>> path. When the user goes to https://example.com/ the default language is
>> rendered and it is the desired behavior. When I switch the URL becomes
>> https://example.com/de/ which is also OK. But now when I try to switch
>> back to English (to the default language), the URL becomes
>> https://example.com/en/ which I don’t want. Trying to set Locale.ROOT as
>> the locale for the default language after switching from German doesn’t
>> work, because "" (which Locale.ROOT effectively represents) isn’t
>> configured in SymbolConstants.SUPPORTED_LOCALES.
>>
>> The only thing I’ve seen are some older threads where PersistentLocale was
>> copied, pasted & adjusted to allow null parameters. Is this workaround the
>> only way to go or is there an official, Tapestry way to not have the locale
>> in the URL path for the default language?
>>
>> Best,
>> Rafael
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Localization & URLs for Default Language

chiefsucker
Hello,

I was comparing a Locale with a String, so please forget my previous message ;)

Everything works as expected now. Thanks for this great idea that is much more lightweight than writing a link transformer.

Best,
Rafael


> On 2019-06-09, at 01:21 PM, Rafael Bugajewski <[hidden email]> wrote:
>
> Hello Cezary,
>
> that’s actually a great idea. I tried to add a service advisor, but it doesn’t seem to do what I want:
>
> @Match("PersistentLocale")
> public static void adviseNullForDefaultLanguage(MethodAdviceReceiver receiver) {
>    MethodAdvice advice = new MethodAdvice() {
>        public void advise(MethodInvocation invocation) {
>            invocation.proceed();
>
>            if (invocation.getReturnValue() != null && invocation.getReturnValue().equals("en")) {
>                invocation.setReturnValue(null);
>            }
>        }
>    };
>
>    for (Method m : receiver.getInterface().getMethods()) {
>        if (m.getName().equals("get"))
>            receiver.adviseMethod(m, advice);
>    }
> }
>
> The basic idea was that a method called "get" on the service called "PersistentLocale" returns null if the return value from the implementation is "en", but actually nothing happens.
>
> Any ideas what I’m doing wrong?
>
> Best,
> Rafael
>
>
>> On 2019-06-09, at 12:10 PM, Cezary Biernacki <[hidden email]> wrote:
>>
>> You can always decorate PersistentLocale and change behaviour of get() by
>> returning null from it, when the original result is your default locale.
>>
>> Cezary
>>
>>
>> On Fri, Sep 6, 2019 at 10:45 AM Rafael Bugajewski <[hidden email]>
>> wrote:
>>
>>> Hello,
>>>
>>> Tapestry supports putting the current locale into the URL path. I’m
>>> injecting PersistentLocale and switching between languages as advised in
>>> the documentation:
>>>
>>> if (persistentLocale.get() == null) {
>>>   persistentLocale.set(new Locale("de"));
>>> } else if ("en".equalsIgnoreCase(persistentLocale.get().getLanguage())) {
>>>   persistentLocale.set(new Locale("de"));
>>> } else {
>>>   persistentLocale.set(new Locale("en"));
>>> }
>>>
>>> The problem is that the default language should not have a prefixed URL
>>> path. When the user goes to https://example.com/ the default language is
>>> rendered and it is the desired behavior. When I switch the URL becomes
>>> https://example.com/de/ which is also OK. But now when I try to switch
>>> back to English (to the default language), the URL becomes
>>> https://example.com/en/ which I don’t want. Trying to set Locale.ROOT as
>>> the locale for the default language after switching from German doesn’t
>>> work, because "" (which Locale.ROOT effectively represents) isn’t
>>> configured in SymbolConstants.SUPPORTED_LOCALES.
>>>
>>> The only thing I’ve seen are some older threads where PersistentLocale was
>>> copied, pasted & adjusted to allow null parameters. Is this workaround the
>>> only way to go or is there an official, Tapestry way to not have the locale
>>> in the URL path for the default language?
>>>
>>> Best,
>>> Rafael
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]