regarding i18n configuration

Splash Forums Rewrite Users regarding i18n configuration

This topic contains 7 replies, has 3 voices, and was last updated by  Lincoln Baxter III 2 years, 4 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #25941

    naska.om
    Participant

    I have a select language page which I want to get open the first time my website is opened on a computer. Then after user chooses his/her preferred language a language cookie is created and this page does not open later. I want it to be myapp/Select_Language
    This works fine with the follwoing configuration:

    .addRule()             
                .when(Path.matches("/"))
                 .perform(new HttpOperation(){
                    @Override
                    public void performHttp(HttpServletRewrite event, EvaluationContext context){
                        HttpServletRequest request =   (HttpServletRequest) event.getRequest();                      
                        if (request !=null){
                            Cookie langCookie = getCookie(request, "language");                        
                            if(langCookie != null){                                   
                                    Redirect.temporary(event.getContextPath() + "/"+ langCookie.getValue() + "/Home").perform(event, context);                             
                                }else{                                   
                                    Redirect.temporary(event.getContextPath() + "/Select_Language").perform(event, context);
                                }                    
                            }else{                                 
                                Redirect.temporary(event.getContextPath() + "/Select_Language").perform(event, context);
                        }
                    }
                 })
    
    .addRule(Join.path("/Select_Language")
                 .to("/Select_Language.xhtml")
                 .withInboundCorrection())

    With the following i18n configuration myapp/en/Home and all other links work but view params don’t get passed with h:link and f:param:

    private static final Map<String, Properties> langToViews = new HashMap<>();
        private static final Map<String, Properties> langToPaths = new HashMap<>();
        
        private String getView(String lang, String path){
            Properties viewMap = getViewMap(lang);
            String view = viewMap.getProperty(path);
            return String.format("/%s.xhtml", view);
        }
        private Properties getViewMap(String lang){
            Properties props = langToViews.get(lang);
            if(props == null){
                props = new Properties();
                try{
                    props.load(getClass().getResourceAsStream(String.format("text_%s.properties",lang)));
                    langToViews.put(lang, props);
                } catch(IOException e){
                    throw new RuntimeException(e);
                }
            }
            return props;
        }
        private String getPath(String lang, String view){
            Properties pathMap = getPathMap(lang);
            String path = pathMap.getProperty(view);
            return String.format("/%s/%s",lang, path);
        }
        private Properties getPathMap(String lang){
            Properties props = langToPaths.get(lang);
            if(props == null){
                props = new Properties();
                try{
                    props.load(getClass().getResourceAsStream(String.format("text_%s.properties",lang)));
                    langToPaths.put(lang, props);
                }catch (IOException e){
                    throw new RuntimeException(e);
                }
            }
            return props;
        }
    
     .addRule()
                 .when(Direction.isInbound().and(Path.matches("/{lang}/{path}")))
                 .perform(new HttpOperation(){
                     @Override
                     public void performHttp(HttpServletRewrite event, EvaluationContext context){                     
                         String lang = (String) Evaluation.property("lang").retrieve(event, context);
                         String path = (String) Evaluation.property("path").retrieve(event, context);
                         String view = getView(lang, path);
                         localeBean.setLang(lang);
                         Forward.to(view).perform(event, context);                     
                     }
                 })             
                 .where("lang").matches("en|ar|ku")             
                 
                 .addRule()
                 .when(Direction.isOutbound().and(Path.matches("/{view}.xhtml")))
                 .perform(new HttpOperation(){
                     @Override
                     public void performHttp(HttpServletRewrite event, EvaluationContext context){
                         String lang = localeBean.getLocale().getLanguage();
                         String view = (String) Evaluation.property("view").retrieve(event, context);
                         String path = getPath(lang, view);
                         Substitute.with(path).perform(event, context);                     
                     }
                 }) 

    While view params work with rules using join like
    `.addRule(Join.path(“/admin/home”)
    .to(“/admin/index.xhtml”)
    .withInboundCorrection()) `

    With this rule using transposedBy not all the links work, it gives NPE

    at java.util.concurrent.ConcurrentHashMap.hash(ConcurrentHashMap.java:333)
    at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:1016)
    at org.ocpsoft.rewrite.transposition.LocaleTransposition.transpose(LocaleTransposition.java:102)
    ...
    

    I hope I’ve been clear enough.

    #25974

    naska.om
    Participant

    I couldn’t get this i18n to work. What could go wrong with the transposition approach that I get those NPEs?

    #25975

    Could you please post the full stack trace? And possibly a sample application that reproduces this issue? Thanks!

    #25976

    naska.om
    Participant

    I finally got it to work by using perform instead of join at 7:am. I would be happy to know the cause for I can’t figure out why join didn’t work.

    #25977

    naska.om
    Participant

    I got another problem though. I can type both myapp/lang/path and myapp/path.xhtml in the address bar and get to path.xhtml.
    Here is my configuration:

    .addRule()
                 .when(Direction.isInbound().and(Path.matches("/{lang}/{path}").withRequestBinding()))
                 .perform(Forward.to("/{path}.xhtml"))
                 //(Join.path("/{lang}/{path}").to("/{path}.xhtml"))             
                 .where("path").transposedBy(LocaleTransposition.bundle("com.myapp.bundles.text", "lang"))             
                 .where("lang").matches("en|ar") 
                 
                 .addRule()
                 .when(Direction.isOutbound().and(Path.matches("{path}.xhtml")))
                 .perform(Substitute.with("/{lang}/{path}"))
                 .where("path").transposedBy(LocaleTransposition.bundle("com.myapp.bundles.text", "lang"))             
                 .where("lang").matches("en|ar")
    #25982

    Yeah, sure, there is nothing that “blocks” accessing the page directly. What do you expect to happen if the page is accessed directly?

    #25984

    naska.om
    Participant

    I want it to redirect to myapp/lang/path/

    #26008

    If you want it to redirect, then you should use Redirect instead of using Substitute in your last .perform() clause. Substitute will perform an internal servlet Forward.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

Comments are closed.