What is translations in laravel? - laravel

I am new to laravel just working on larapay template i want to know about translations.
Below is my code to create one label and input on form:
<div class="form-group">
{!! Form::label('hsnsac',trans('HSN/SAC').' '.trans('code'),[])!!}
{!! Form::input('number','hsnsac',isset($item) ? $item->hsnsac : '',['class'=>'form-control','placeholder'=>trans('HSN/SAC').' '.trans('messages.code')])!!}
</div>
Below is default code for display elements:
<div class="form-group">
{!! Form::label('code',trans('messages.item').' '.trans('messages.code'),[])!!}
{!! Form::input('text','code',isset($item) ? $item->code : '',['class'=>'form-control','placeholder'=>trans('messages.item').' '.trans('messages.code')])!!}
</div>
i cant understand what is trans(messages) in code.
and if i used my own code like trans(messages.hsnsac) it will display this messages.hsnsac on form.

trans is a shortcut helper to the lang directory in your resources directory.
If your lang is set to en it will attempt to derive from this. Because the notation is dot based, the first word will be the file name, the second will be the key, like this:
resources
|
| ---- lang
|
| ---- en
|
| ---- messages.php
Now you need to return an array fro this file.
return [
'hsnsac' => 'This text will appear now';
];
Finally, if you call trans('messages.hsnsac') it will return This text will appear now.
Mind you, this is only for en. If you are using another language, make sure the file exists in a directory for that lang under resources/lang as well.

You need to define these translation strings.
Create the file resources/lang/{language}/messages.php and add your translations here.

Related

Laravel Blade: What is nesting sections doing here?

I don't know what does putting a section inside another section do. In a Laravel project I'm reading, a Blade file's code is like this:
login.blade.php :
#extends('layout') #section('content')
#section('title', 'Log in')
Lots of content.
#endsection
In above, what's the point of having title section inside content section? How will it be different if title section is placed outside:
#extends('layout')
#section('title', 'Log in')
#section('content')
Lots of content.
#endsection
I tested, and both are producing same output (HTML source code).
layout.blade.php
<head><title>#yield('title')</title></head>
<body>#yield('content')</body>
Is there any case of layout.blade.php in which different outputs will be produced?
The section directive simply copies whatever you have within #section and #endsection to the name #yield place holder on the extended template.
This means it does not consider where its placed, but for clarity and readability, the second example is the right structure.
For example, this shows that the order of the directive doesn't matter
In welcome.blade.php
#extends('default')
#section('a')
This is a
#section('c', 'This is c')
#section('b')
This is b
#section('d', 'This is d')
#endsection
#endsection
In default.blade.php
#yield('c')
#yield('a')
#yield('b')
#yield('d')
The output
This is c This is a This is b This is d
The best practice is to make your code more readable by opening and closing each block:
#extends('default')
#section('a')
This is a
#endsection
#section('c', 'This is c')
#section('b')
This is b
#endsection
#section('d', 'This is d')
It's better to put them on different rows for readability sake.
First section just puts "Log in" wherever the section "title" is:
#section('title', 'Log in')
Second section has the beginning and the end and is selfdescriptive.

Thymeleaf Template not rendering param value inside th if condition

I am using thymeleaf template for my spring boot application. Here below the main page,
<div th:replace="content :: content"></div>
and inside content fragment,
<div th:fragment="content">
<h4 th:if="${param.val== 'abc'}">SOME-TEXT</h4> // not working
<h4 th:if="${param.val== 'abc'}" th:text="${param.val}"></h4> // not working
<h4 th:text="${param.val}"></h4> // working and value is abc
<h4 th:unless="${param.val== 'abc'}" th:text="${param.val}"></h4> // working - value in html text is abc
<h4 th:unless="${param.val== 'abc'}">SOME-TEXT</h4> // Working, value is SOME-TEXT
</div>
URL: domain/?val=abc
I want to display: SOME-TEXT in html if param.val == 'abc'.
Value 'abc' is coming inside th:text. But inside th:if failing.
Seems some hidden extra strings added to param.val?
Any suggestion?
The Thymeleaf function ${param.val} will return a request parameter called val. But this could be a multivalued object (e.g. an array) - for example consider this (which is a valid construction):
?val=abc&val=def
So to work with a single-valued string, you can do this:
<h4 th:if="${#strings.toString(param.val)} == 'abc'" th:text="'SOME-TEXT-2'">SOME-TEXT-1</h4>
This prints SOME-TEXT-2 in the web page.
Or you can use this:
<h4 th:if="${#strings.toString(param.val)} == 'abc'">SOME-TEXT-1</h4>
Which prints SOME-TEXT-1.
Just out of interest, if you used that first example val=abc&val=def, then you can see what happens with this:
<h4 th:text="${param.val}"></h4>
It prints an array:
[abc, def]
You may see something like this when processing a series of related checkboxes (just as one example).
Update:
For a null-check, using Thymeleaf, you can do this:
<h4 th:if="${param.val} != null and
${#strings.toString(param.val)} == 'abc'">SOME-TEXT-2</h4>
In this specific case, it isn't really needed, as you are not doing anything with the null value which might cause a problem.
It's more relevant if you are chaining values in objects foo.bar.baz - and you need to check if foo or bar are null to avoid a null pointer exception.
Bear in mind that Spring's expression language has the safe navigation operator, which can be very helpful in such cases: foo.?bar.?baz, allowing you to write more concise null handling than with Thymeleaf alone. But again, not relevant to your specific example from the question.

jekyll filename for current file

The title says it all. Is there a way to access the current filename in Jekyll? Before you jump the gun and try to mark it as duplicate, keep in mind that I'm asking for the current filename, not for the generated filepath which can be access by page.url.
As an example, lets say that there is a file1.html which include file2.html. I want to be be able to get file1.html if I'm inside file1.html and file2.html vice-versa. page.path only returns the filename for the generated file as in I will get file1.html even from within file2.html.
A concrete example:
file2.html:
<div>
{{ page.name }}
</div>
file1.html:
<div>
{% include file2.html %}
</div>
index.html:
{% include file1.html %}
rendered:
<div>
<div>
index.html
</div>
</div>
Instead of index.html, I want it to return file2.html
Thanks
Edit : An include is just a fragment that is inserted into a page. Once every include are made, the page is then processed as a single block of code.
There is no way to get any info from liquid about an include name.b

Ruby Mechanize, save html as file after filling a form

I want to save the html after filling a form. lets say:
page.form.field.value = 'testing'
page.save 'test.html'
the generated test.html file don't have the modified value attribute
<input name='something' value=''>
I'm expecting:
<input name='something' value='testing'>
You want to use dom functions for that:
page.at('[name=something]')['value'] = 'testing'
In other words there's no reason to expect that changes to the Form fields will update the dom.

Magento - Limit title length for seo

I'd really like to limit the title length on products in Magento.
What I've tried is adding 'maxlength' => 65 somewhere in \app\code\core\Mage\Adminhtml\Block\, without success.
Does someone know how to add this feature? In HTML it will just be adding length="65" maxlength="65".
Thanks for all affords. :)
I don't have the platform available to give you a proper walkthrough on setting this up, but I should be able to get you in the right place. First of all, do not make changes in the app/code/core files. Any changes you would absolutely need to make to those files you should do by making a copy of lets say app/code/core/Mage/Sales/something.php to app/code/local/Mage/Sales/something.php. Magento knows to automatically use the code in local to override the code in core.
If you take a look at the source code for that page you'll see where the name form is:
<input id="name" name="product[name]" value="" class=" required-entry input-text required-entry" type="text"/> </td>
<td class="scope-label"><span class="nobr">[STORE VIEW]</span></td>
</tr>
What's going on here is you'll notice under class we have "required-entry input-text and, well, required-entry again. These are the validation tags defined in js/prototype/validation.js. You will need to add some custom validation to it, and add it to your template file (not in core, it can break when you upgrade).
You'll notice in validation.js a section
Validation.add('IsEmpty', '', function(v) {
In this section you can add your custom validation. Lets say:
//make sure these are unique, I'm not checking
['validate-length', 'Your input needs to be less than x characters.', function(v) {
if (v.length > x) return false;
}],
If you need help finding the template location, take a look at: Finding Correct Templates and Blocks in Magento. You'll simply add validate-length class such as: class="required-entry validate-length..."
After almost 10 hours of searching I gave the up the "best" way, and choose for the roundabout.
Simply add
document.getElementById("name").setAttribute("maxlength", "65");
document.getElementById("name").setAttribute("length", "65");
to app/design/adminhtml/default/default/template/catalog/wysiwyg/js.phtml
You can add javascript validator to the product's name attribute. To achieve this, you need to update attribute with special value for frontend class. Just create sql upgrade:
$this->updateAttribute(
Mage_Catalog_Model_Product::ENTITY,
'name',
array(
'frontend_class' => 'validate-length maximum-length-65',
'note' => 'Max length is 65 characters'
)
);

Resources