Localize flash messages? - localization

Please advise how to localize flash messages? I followed laracast series: https://laracasts.com/series/build-project-flyer-with-me/episodes/9
Auth::login(User::firstOrCreate($data));
flash()->success('flash.success', 'flash.login');
return redirect($this->redirectPath());`

return redirect('/login')->with('status', trans('auth.registered'));
In resources/lang/es/auth.php
return [
...
'registered' => "Your message translated on Spanish.",
];

Go through about localization Laravel Localization
You can find more information

You can do that
in your controller
return redirect('/login')->with('success', __('auth.registered'));
and in lang files for example using En and Ar
go to lang/en/auth and create your status message
'registered' => 'Your message in English'
go to lang/ar/auth and create your status message
'registered' => 'Your message in Arabic'
then in your blade, you retrieve the coming message from the controller
#if(session('success'))
<div class="alert alert-success" role="alert">
<div class="alert-body">
{{session('success')}}
</div>
</div>
#endif

Related

Why won't my Angular app send params to my Entangle rails controller?

In an effort to learn about realtime apps, I am trying to create a basic messageboard using Entangled.
I have an Angular app and a Rails backend with a Messages Controller that includes Entangled::Controller. This Rails controller successfully receives a request when a form is submitted from the Angular app - the form is submitted using Entangled. (On clicking submit, a function is triggered in an Angular controller which should create a new message in the backend and update all clients subscribed to that backend.)
I know the Angular function is being triggered on clicking submit, and I know the function receives the correct information from the form: console.log($scope.message) displays {socket: "ws://message-board-olliehm.c9users.io:8080/messages", username: "ggg", content: "gggggg"} where I submit "ggg" in the username field and "gggggg" in the content field.
The problem is that these fields are not arriving at the Rails controller. When I click submit, the correct action is triggered in the Rails controller, but the params don't contain the right information: p params in def create returns {"controller"=>"messages", "action"=>"create"}, with no "message" hash and no "username" or "content" keys.
I cannot work out what Entangled is doing with the username and content fields.
Redis is new to me so I'm not sure if the problem is there. I have Redis installed and the redis-server is running as required by Entangled. I have a redis initializer as below, which in the Rails console is successfully connecting and letting me put data in the database:
$redis = Redis.new(:host => $IP, :port => 6379)
Here's my Angular controller:
var controllers = angular.module('controllers');
controllers.controller('MessageboardController', ['$scope','Message', function($scope,Message){
$scope.message = Message.new();
$scope.post = function() {
console.log($scope.message);
$scope.message.$save(function() {
$scope.$apply(function() {
$scope.message = Message.new();
});
});
};
Message.all(function(err, messages) {
$scope.$apply(function() {
$scope.messages = messages;
});
});
}]);
Message here refers to this factory:
messageboard.factory('Message', function(Entangled){
return new Entangled('ws://message-board-olliehm.c9users.io:8080/messages');
});
And here's my Angular view:
<h1>Messageboard</h1>
<section class='row' ng-if='messages'>
<ul>
<li ng-repeat='message in messages'>
{{message.username}}
{{message.content}}
</li>
</ul>
</section>
<section class='row'>
<form ng-submit="post()">
<div class='form-group'>
<label for='username'>Message as</label>
<input ng-model='message.username' name='username' type='text'>
</div>
<div class='form-group'>
<input ng-model='message.content' name='message' type='text' placeholder='Write your message here'>
</div>
<div class='form-group'>
<input type="submit">
</div>
</form>
</section>
Advice would be hugely appreciated - this has caused prolonged frustration and I'm very keen to get stuck into creating something with realtime updates.
After much trial and error I realised the empty params apparently received by the backend controller were misleading. The params arrive properly and a new message object is created and broadcast as long as I do the following to the raw params the controller receives:
params.require(:message).permit(:content,:username)
i.e. exactly what you'd do with the form params arriving as you'd expect. The weird thing for me was that params doesn't include a 'message' hash until you require that hash as above. I guess this is something to do with it not being a regular http request, though a proper explanation would be appreciated...

Rails custom client side validation message

I'm using a rails form with client side validations. How can I customize the error message, which current says:
"Value must be equal to or greater than ... "
Here is the form field:
<%= f.number_field :age, placeholder:"Age", class:"form-control", required: true, max:90, min:17, message: 'foo' %>
HTML5 API has a way to set custom error validation message. Look below the example:
Example:
<form>
<label for="mail">I would like you to provide me an e-mail</label>
<input type="email" id="mail" name="mail">
<button>Submit</button>
</form>
And then adding a JS:
var email = document.getElementById("mail");
email.addEventListener("keyup", function (event) {
if (email.validity.typeMismatch) {
email.setCustomValidity("I expect an e-mail, darling!");
} else {
email.setCustomValidity("");
}
});
Documentation of setCustomValidity().

asp-validation-summary shows custom but not model errors

Asp-validation-summary in view shows errors added via ModelState.AddModelError, but don't show any model validation errors ("The User field is required.").
I'm using Microsoft.AspNetCore.Mvc 1.0.0-rc2-final.
Btw: User field is not displayed via the view but correctly identified by EF as a model level error before add.
//<div asp-validation-summary="ModelOnly" class="text-danger"></div> in view
ModelState.AddModelError(string.Empty, "This error shows up in validation-summary");
ViewBag.HeaderMessage = "Error: " + string.Join(" - ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
Errors show in field filled via ViewBag but not in validation-summary
Change your html:
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
to
<div asp-validation-summary="All" class="text-danger"></div>

Rails 4 with Angular JS encounter Controller is not a function [ng:areq] error

I had prepared a feedback system in the site. User can submit some feedback in a box and submit via Angular JS. It was working well but when I check the system a week ago, it does not work. When submitting the text, the page was reloaded instead of sending off the text. When looking at the Chrome Console, it stated:
Error: [ng:areq] Argument 'FeedbackController' is not a function, got undefined
I have no idea what this error means. I tried to use Postman to test if the server side does work but Postman does not have the authorization InvalidAuthenticityToken. At the moment I am stuck and don't know what can I do to debug this problem. Any idea would be very much appreciated!
Feedback.js.coffee:
#app = angular.module("FeedbackBox", ["ngResource"])
app.factory "Feedback", ["$resource", ($resource) ->
# It should not be update?
$resource("/feedbacks/:id", {id: "#id"},
{
update: {method: "PUT"} ,
})
]
#FeedbackController = ["$scope", "Feedback", ($scope, Feedback) ->
## $scope.messages = Feedback.query()
#console.log "Within the Controller declaration!"
$scope.addFeedback = ->
console.log "Running addFeedback()"
$scope.newFeedback.feedback_path = document.URL
# console.log $scope.newFeedback
Feedback.save($scope.newFeedback)
## Showing "Thank you!" after submission */
$("#messageBox textarea").hide()
$("#messageBox input:submit").hide()
$("#messageBox .thanks").show()
## Hiding box after 3 seconds */
setTimeout (->
$("#messageBox .mainArea").removeClass "open"
$("#messageBox textarea").show()
$("#messageBox input:submit").show()
$("#messageBox .thanks").hide()
return
), 3000
$scope.newFeedback = {}
]
feedbacks_controller.rb
def create
respond_with Feedback.create(feedback_params)
end
UPDATE
And here is the _feedbackBox.html.erb where I register the Controller:
<div id="messageBox" ng-app="FeedbackBox" ng-controller="FeedbackController">
<div class="handle"><%= t("tab_text", scope: 'feedbacks.messageBox')%></div>
<div class="mainArea">
<form ng-submit="addFeedback()">
<p><%= t("question_text", scope: 'feedbacks.messageBox')%></p>
<% unless signed_in? %>
<input type="text" placeholder="<%= t("name_placeholder", scope: 'feedbacks.messageBox')%>" ng-model="newFeedback.sender_name">
<% end %>
<div class="thanks" style="display: none; height: 200px;width: 340px;text-align: center;padding-top: 91px;"><%= t("thankyou_text", scope: 'feedbacks.messageBox')%></div>
<textarea placeholder="<%= t("feedback_placeholder", scope: 'feedbacks.messageBox')%>" ng-model="newFeedback.content"></textarea>
<input type="submit" value="<%= t("submit_button", scope: 'feedbacks.messageBox')%>">
</form>
</div>
</div>
The structure is very simple... Any idea? Thanks
Following #PSL suggestion, the problem arose because the angular-js version is upgraded from 1.2.x to 1.3.x. I will have to declare the Controller by adding the following line:
app.controller("FeedbackController", FeedbackController)
Everything works just fine now! Thanks #PSL!

flash.message does not print message on the screen in grails 2.x

My functionality is I have a list of users. When I click on any of the users to edit his information a new window pop up.
I change some values and click on save. Now values are saved correctly but 'saved successfully' message is not displayed.
My controller code is
if(user.save(flush:true)){
user.messages = "${message(code: 'user.saved')}"
flash.userInstance = user
render ...
}
and in gsp I print message like this
<div id="messages">
<g:if test="${userInstance?.messages}">
<div class="message">${userInstance?.messages}</div>
</g:if>
<div>
Here Call does not go inside g:if condition hence message is not printed.
This was working well in grails 1.3 but fails in 2.2. Why is that so?
This example works for me. Maybe you have some validation errors, try to debug your code.
if(user.save(flush:true)){
user.messages = "${message(code: 'user.saved')}"
flash.userInstance = user
render ...
}
and in gsp
<div id="messages">
<g:if test="${flash?.userInstance?.messages}">
<div class="message">${flash?.userInstance?.messages}</div>
</g:if>
<div>

Resources