How to split messages file into multiple files in play 2.0 framework
Asked Answered
H

2

7

I have a huge message file which i need to split into multiples files for different languages. For example :

I created one folder for English locale i.e. en and another for French locale , fr inside conf folder. en contains messages1_en.properties and messages2_en.properties fr contains messages1_fr.properties and messages2_fr.properties

How to access these properties files inside my view.

Thanks

Huertas answered 8/11, 2013 at 7:41 Comment(0)
S
0

The only way to do that without introducing your own alternative implementation and use that instead of the built in Messages is to use hacked locales, so you would do fr_type1, fr_type2 or something like that to select the right alternative.

This is probably a bad idea since it's always risky to use an API in a different way from how it was intended to be used, there is a high risk of unexpected behaviour and it might be brittle since there is no guarantee that you will be able to use made up locales in future versions etc.

If you look at the Messages implementation you could probably get some ideas of how to implement your own without much fuss.

Good luck!

Samara answered 8/11, 2013 at 9:7 Comment(3)
I am trying to understand the Messages implementation but i am unable to understand as when and which Messages class is called.In framework/src , there is one Messages.java , in scala also Message class is there and i can see multiple Messages and Messages$ class files in play 2.10 jar..I am totally confused...please help meHuertas
Kindly confirm whether transformation of labels in any other language in play 2.0 internationalization feature is server or client side job.Huertas
There is a Java Messages implementation that just calls the Scala implementation. The transformations are done server side.Samara
A
0

That's an old question, but i had a close issue, and i didn't find a solution anywhere.

This example use a configuration key to load messages from a file with a custom name. But you can easily modify it to load messages file from a subdirectory and/or multiple messages files.

  1. Override play.api.i18n.DefaultMessagesApiProvider

    @Singleton
    class CustomMessagesApiProvider  @Inject() (
                                                environment: Environment,
                                                config: Configuration,
                                                langs: Langs,
                                                httpConfiguration: HttpConfiguration)
      extends DefaultMessagesApiProvider(environment, config, langs, httpConfiguration) {
    
    
      def filename =
        config.get[String]("play.i18n.filename")
    
      override protected def loadAllMessages: Map[String, Map[String, String]] = {
        langs.availables.map(_.code).map { lang =>
          (lang, loadMessages(filename +"."  + lang))
        }.toMap
          .+("default" -> loadMessages(filename))
          .+("default.play" -> loadMessages(filename+".default"))
      }
    
    }
    
  2. Add Guice binding in Module.java

    @Override
    public void configure() {
        bind(DefaultMessagesApiProvider.class).to(CustomMessagesApiProvider.class);
    }
    

It's my first Scala class, so maybe it can be improved. But it works.

To load multiple files (it compiles but not tested)

    override protected def loadAllMessages: Map[String, Map[String, String]] = {
      langs.availables.map(_.code).map { lang =>
        (lang,
          loadMessageFiles("." + lang))
      }.toMap
        .+("default" -> loadMessageFiles(""))
        .+("default.play" -> loadMessageFiles(".default"))
    }

    private def loadMessageFiles(suffix: String) = {
      loadMessages("messages-1" + suffix) ++ loadMessages("messages-2" + suffix)
    }
Adminicle answered 5/7, 2018 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.