{"id":13695666,"url":"https://github.com/julienrf/play-jsmessages","last_synced_at":"2025-05-08T21:25:32.095Z","repository":{"id":2956301,"uuid":"3970418","full_name":"julienrf/play-jsmessages","owner":"julienrf","description":"Library to compute localized messages of your Play application on client side","archived":false,"fork":false,"pushed_at":"2023-12-15T15:48:28.000Z","size":175,"stargazers_count":124,"open_issues_count":2,"forks_count":32,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-02-02T03:51:17.059Z","etag":null,"topics":["i18n","javascript","playframework"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"ChristosChristofidis/awesome-deep-learning","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/julienrf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["julienrf"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2012-04-09T09:10:20.000Z","updated_at":"2024-01-23T07:43:50.000Z","dependencies_parsed_at":"2024-10-23T03:23:50.845Z","dependency_job_id":null,"html_url":"https://github.com/julienrf/play-jsmessages","commit_stats":{"total_commits":99,"total_committers":13,"mean_commits":7.615384615384615,"dds":"0.33333333333333337","last_synced_commit":"0a8bf6c6db4cdefd3d28521bcc2d9996491b62af"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julienrf%2Fplay-jsmessages","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julienrf%2Fplay-jsmessages/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julienrf%2Fplay-jsmessages/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/julienrf%2Fplay-jsmessages/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/julienrf","download_url":"https://codeload.github.com/julienrf/play-jsmessages/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238044095,"owners_count":19407128,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["i18n","javascript","playframework"],"created_at":"2024-08-02T18:00:32.065Z","updated_at":"2025-02-10T02:09:30.990Z","avatar_url":"https://github.com/julienrf.png","language":"Scala","funding_links":["https://github.com/sponsors/julienrf"],"categories":["国际化","Libraries"],"sub_categories":["文件同步","Others"],"readme":"# Play JsMessages library\n\nThis library allows you to compute localized messages on client-side, in Play projects.\n\nBasically, play-jsmessages takes the i18n messages of your Play application, sends them to the client-side as a JSON object and defines a JavaScript function returning a message value from a given language and the message key and arguments.\n\nTake a look at the [Scala](/sample-scala) and [Java](/sample-java) samples to see it in action.\n\n## Installation (using sbt)\n\nAdd a dependency on the following artifact:\n\n```scala\nlibraryDependencies += \"org.julienrf\" %% \"play-jsmessages\" % \"7.0.0\"\n```\n\nThe current 7.0.0 version is compatible with Play 3.0 and Scala 2.13 and 3.3.\n\nPrevious versions are available here:\n * [`6.0.0`](https://github.com/julienrf/play-jsmessages/tree/v6.0.0) for play-2.9 ;\n * [`5.0.0`](https://github.com/julienrf/play-jsmessages/tree/v5.0.0) for play-2.8 ;\n * [`4.0.0`](https://github.com/julienrf/play-jsmessages/tree/v4.0.0) for play-2.7 ;\n * [`3.0.0`](https://github.com/julienrf/play-jsmessages/tree/v3.0.0) for play-2.6 ;\n * [`2.1.0`](https://github.com/julienrf/play-jsmessages/tree/2.1.0) for play-2.5 ;\n * [`2.0.0`](https://github.com/julienrf/play-jsmessages/tree/2.0.0) for play-2.4 ;\n * [`1.6.2`](https://github.com/julienrf/play-jsmessages/tree/1.6.2) for play-2.3 ;\n * [`1.6.1`](https://github.com/julienrf/play-jsmessages/tree/1.6.1) for play-2.2 ;\n * [`1.5.2`](https://github.com/julienrf/play-jsmessages/tree/1.5.2) for play-2.2 ;\n * [`1.5.0`](https://github.com/julienrf/play-jsmessages/tree/1.5.0) for play-2.1 ;\n * [`1.3`](https://github.com/julienrf/play-jsmessages/tree/403dc8d7248c965c827b70edeff55016ae274bef) for play-2.0 ;\n * [`1.2.1`](https://github.com/julienrf/play-jsmessages/tree/cc52b23dae9997b77da3cbccf6d22b60a557c2ee) for play-2.0.\n\n## API Documentation\n\nYou can browse the online [scaladoc](https://www.javadoc.io/doc/org.julienrf/play-jsmessages_2.12).\n\n## Quick start\n\n### Select which messages you want to support on client-side\n\n#### Get a `JsMessagesFactory`\n\nThe `JsMessagesFactory` class is the starting point: it allows you to select which messages of your application\nyou want to support on client-side. You can use all the messages of the application or just a subset of them.\n\nThe simplest way to get a `JsMessagesFactory` is _via_ dependency injection:\n\n```scala\nimport jsmessages.JsMessagesFactory\nimport javax.inject.Inject\n\nclass Application @Inject() (jsMessagesFactory: JsMessagesFactory) extends Controller {\n\n}\n```\n\nThe equivalent Java code is the following:\n\n```java\nimport jsmessages.JsMessagesFactory;\nimport javax.inject.Inject;\n\npublic class Application extends Controller {\n\n  @Inject\n  public Application(JsMessagesFactory jsMessagesFactory) {\n\n  }\n\n}\n```\n\nThe constructor of the `JsMessagesFactory` itself needs to be injected a `play.api.i18n.MessagesApi` parameter. By default,\nPlay defines a binding that uses a `play.api.i18n.DefaultMessagesApi`, but you can disable this binding if you want to use something else.\n\nFor more control, you can also manually instantiate your `JsMessagesFactory`:\n\n```scala\nval jsMessagesFactory = new JsMessagesFactory(someMessagesApi)\n```\n\nThe equivalent Java code is the following:\n\n```java\nJsMessagesFactory jsMessagesFactory = new JsMessagesFactory(someMessagesApi);\n```\n\nNote that, in Scala, you may want to use the `JsMessagesFactoryComponents` trait that provides a `jsMessagesFactory: JsMessagesFactory`\nmember using an abstract `messagesApi: MessagesApi` member.\n\n#### Select which messages to use\n\nYou can either use all your i18n messages:\n\n```scala\nval jsMessages = jsMessagesFactory.all\n```\n\nOr, just an extensive subset:\n\n```scala\nval jsMessages = jsMessagesFactory.subset(\"error.required\", \"error.number\")\n```\n\nOr, finally, an intensive subset:\n\n```scala\nval jsMessages = jsMessagesFactory.filtering(_.startsWith(\"error.\"))\n```\n\nThe equivalent Java code of the above expressions are the following:\n\n```java\nimport jsmessages.JsMessages;\nimport play.libs.Scala;\nJsMessages jsMessages = jsMessagesFactory.all();\nJsMessages jsMessages = jsMessagesFactory.subset(Scala.varargs(\"error.required\", \"error.number\"));\nJsMessages jsMessages = jsMessagesFactory.filtering(new scala.runtime.AbstractFunction1\u003cString, Boolean\u003e() {\n  @Override\n  public Boolean apply(String key) {\n    return key.startsWith(\"error.\");\n  }\n});\n```\n\n### Generate a JavaScript asset …\n\n#### … for the client’s preferred language\n\nThen, you typically want to define an action returning a JavaScript resource containing all the machinery to compute\nlocalized messages from client-side, using the client’s preferred lang:\n\n```scala\nval messages = Action { implicit request =\u003e\n  Ok(jsMessages(Some(\"window.Messages\")))\n}\n```\n\nNote that for this to work the `apply` method of `jsMessages` needs to have an implicit `play.api.i18n.Messages` value. You can\nget one by mixing the `play.api.i18n.I18nSupport` trait in your controller.\n\nOr in Java:\n\n```java\n@Inject\nprivate MessagesApi messagesApi;\n\npublic Result messages(Http.Request request) {\n    return ok(jsMessages.apply(Scala.Option(\"window.Messages\"), this.messagesApi.preferred(request)));\n}\n```\n\nThe above code creates a Play action that returns a JavaScript program containing the localized messages of the\napplication for the client language and defining a global function `window.Messages`. This function returns a localized\nmessage given its key and its arguments:\n\n```javascript\nconsole.log(Messages('greeting', 'Julien')); // will print e.g. \"Hello, Julien!\" or \"Bonjour Julien!\"\n```\n\nThe JavaScript function can also be supplied alternative keys that will be used if the main key is not defined. Keys will be tried in order until a defined key is found:\n\n```javascript\nalert(Messages(['greeting', 'saluting'], 'Julien'));\n```\n\nThe JavaScript function stores the messages map in a `messages` property that is publicly accessible so you can update the messages without reloading the page:\n\n```javascript\n// Update a single message\nMessages.messages['greeting'] = 'Hi there {0}!';\n// Update all messages\nMessages.messages = {\n  'greeting': 'Hello, {0}!'\n}\n```\n\n#### … for all the languages\n\nAlternatively, you can use the `all` method to generate a JavaScript program containing all the messages of the application\ninstead of just those of the client’s current language, making it possible to switch the language from client-side\nwithout reloading the page:\n\n```scala\nval messages = Action {\n  Ok(jsMessages.all(Some(\"window.Messages\")))\n}\n```\n\nThe equivalent Java code is the following:\n\n```java\npublic Result messages() {\n    return ok(jsMessages.all(Scala.Option(\"window.Messages\")));\n}\n```\n\nIn this case, the generated JavaScript function takes an additional parameter corresponding\nto the language to use:\n\n```javascript\nconsole.log(Messages('en', 'greeting', 'Julien')); // \"Hello, Julien!\"\nconsole.log(Messages('fr', 'greeting', 'Julien')); // \"Bonjour Julien!\"\n```\n\nMoreover, the `messages` property of the JavaScript function is a map of languages indexing maps of messages.\n\nThe JavaScript function can also be partially applied to fix its first parameter:\n\n```javascript\nval messagesFr = Messages('fr'); // Use only the 'fr' messages\nconsole.log(messagesFr('greeting', 'Julien')); // \"Bonjour Julien!\"\n```\n\nNote: if you pass `undefined` as the language parameter, it will use the default messages.\n\n## Changelog\n* 7.0.0\n  - Play 3.0.x compatibility.\n \n* 6.0.0\n  - Play 2.9.x compatibility.\n\n* 5.0.0\n  - Play 2.8.x compatibility.\n\n* 4.0.0\n  - Play 2.7.x compatibility, cross-compiled for Scala 2.13.\n\n* 3.0.0\n  - Play 2.6.x compatibility. All tests moved to scalatest+play and dependency injection.\n\n* 2.0.0\n  - Play 2.4.x compatibility.\n\n* 1.6.2\n  - Play 2.3.x compatibility.\n\n* 1.6.1\n  - Fix crash when `undefined` is passed as the language parameter (thanks to Paul Dijou).\n\n* 1.6.0\n  - Big changes in the API ;\n  - Make it possible to return messages of all languages (thanks to Paul Dijou) ;\n  - Discard methods returning HTML, keep just JavaScript.\n\n* 1.5.2\n  - Export the `messages` property on client-side (thanks to Paul Dijou).\n\n* 1.5.1\n  - Play 2.2.x compatibility.\n\n* 1.5.0\n  - Fix the `subset` method of the Java API (thanks to Seppel Hardt) ;\n  - Refactor the whole API in order to make it more extensible (thanks to Jacques Bachellerie).\n\n## License\n\nThis content is released under the [MIT License](http://opensource.org/licenses/mit-license.php).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjulienrf%2Fplay-jsmessages","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjulienrf%2Fplay-jsmessages","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjulienrf%2Fplay-jsmessages/lists"}