Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:39:37+00:00 2026-06-01T17:39:37+00:00

Short version: Is there an attractive, simple way of implementing a language-specific url structure,

  • 0

Short version: Is there an attractive, simple way of implementing a language-specific url structure, ala. host.com/en/ or host.com/pt/ in Play 1.2.5?

I’m looking for a solution that doesn’t require changing all controllers, and the change should preferably only be to the routing file, as I don’t want two sets of redundant views – one for each language.

I already have a message.pt and message.en, and I have a way of switching between the two, but I’m a newb when it comes to routing.

I realize that the routing file can contain a route like: “/(en|pt)/” but I’m kinda stuck here.

I appologies if the question is a bit vague.
I’ll gladly clarify should the need arise.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:39:39+00:00Added an answer on June 1, 2026 at 5:39 pm

    I found a solution in some old change notes for Play 1.1:
    http://www.playframework.org/documentation/1.2/releasenotes-1.1 (under “Global route arguments”). Odd that it isn’t mentioned anywhere else.

    [Edited to include the entire working solution]
    [Edit2 – Fixed a problem]

    My routes file look like this:

    # Home page
    GET     /{lang}/?                           Application.index
    GET     /                                   Application.redirect
    GET     /{lang}/woops/?                     Application.underConstruction
    

    The language controller looks like this atm:

    public class Language extends Controller {
    
    @Before
    static void detectLangInURL() {
        String url = request.url;
    
        if (url.length() > 2 && isSupported(getFromUrl(url)))
        {
            String lang = getFromUrl(url);
            if (!Lang.get().equals(lang)) {
                Lang.change(lang);
                response.setCookie("lang", lang, "60d");
            }
            renderArgs.put("lang", Lang.get());
            routeArgs.put("lang", Lang.get());
        }
    }
    
    private static String getFromUrl(String url) {
        String[] s = url.split("[/]");
        if (s.length > 1)
            return s[1];
        else return "";
    }
    
    private static boolean isSupported(String lang) {
        if (lang.length() == 0)
            return false;
    
        String[] langList = Play.configuration.get("application.langs").toString().split(",");
        for (int i = 0; i < langList.length; i++) {
            if (langList[i].compareToIgnoreCase(lang) == 0)
                return true;
        }
    
        return false;
    }
    }
    

    The redirect method looks like this:

    public static void redirect() {
        Cookie lang = request.current().cookies.get("lang");
        if (lang != null && !lang.value.isEmpty())
            Lang.change(lang.value);
        redirect(Lang.get() + "/");
    }
    

    I had to choose between using a @With(Language.class) annotation or changing the Controller super class. I choose the @With annotation.

    I’m pleased, it ended up being pretty simple.
    I hope someone can benefit from the above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short version: Is there a simple, built-in way to identify the calling view in
Short version: Is there a simple way to get the value of a primary
Short Version: Is there a/what is the suggested way to return error details to
The short version: is there a way to to write an and or an
Short version: Is there a way to check-in a change to a git repository
Short Version Is there a way to force (or provide a hint to) Microsoft
Is there a cleaner way to get the short version hash of HEAD from
Short version: Is there way to achieve in Python the same effect achieved by
Short version Is there a way to prevent R from ever using any virtual
Short version: When two lists are adjacent to each other, is there a way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.