I'm trying to translate a custom ModelAdmin menu title as such:
private static $menu_title = _t('CMS.MyModelAdmin', 'My Model Admin');
This results in a 500 error, whereas a static string works without issues. I'm not sure why since I assumed this variable isn't a database entry.
How can you translate the menu title, preferably using _t?
I've not translated V3, however looking at...
http://api.silverstripe.org/3.1/source-class-LeftAndMain.html#665
$title = _t("{$menuItem->controller}.MENUTITLE", $defaultTitle);
This would indicate to me that if you have a string defined along with the other lang strings in the YML format like this (but for all required languages)...
mysite\lang\en_GB.yml
en_GB:
MyAdmin:
MENUTITLE: 'MyTitle'
(where "MyAdmin" is the name of the "class MyAdmin extends ModelAdmin...")
Related
I've got a ZF2 project with basic/skeleton structure. I've got /var/www/project/module/Application/language with a languages and
/var/www/project/config/autoload/dictionaries.php which contain month names and so on.
I need to translate values of dictionary to desired language. I have tried to make it the same way as I did inside View /var/www/project/module/Application/view/application/module-name/×××.phtml:
$this->plugin('translate')->getTranslator()->setLocale('en_US');
$this->translate("foreign language staff");
But $this have no idea what I'm talking about. Then I try the way I try it inside Controllers /var/www/project/module/Application/src/Application/Controller/ModuleController.php:
use Zend\I18n\Translator\Translator;
class CompanyController extends AbstractActionController {
public function indexAction() {
$translator = new \Zend\I18n\Translator\Translator;
define('LOCALE', substr($translator->getLocale(),0,5));
$translator->setLocale(LOCALE);
$translator->addTranslationFile('gettext', '/....../language/' . LOCALE . '.mo', 'messages', LOCALE);
$translator->translate("foreign language staff"),
But no in the dictionary neither in Controller I can not get translate, even addTranslationFile file path and LOCALE is correctly settled.
I am working on cross browser testing and each of test methods in multiple classes run on 4 browsers Chrome, Firefox, IE, Safari.
The testng HTML reports & extent reports generated have the test methods in a column but I also need the browser name against each test method.
Even if the testng HTML reports would have the browser name against the test method would be great.
I found this link but I just need the browser column next to method column to custom report in the link
You can do it like here. But it would be better to use reporting features for that e.g. you may pass any test name and description to report see docs.
You can do that by creating a customized TestHTMLReporter . Pass any data in your CustomReport.java class and generate your own report like below. I have also explained it here
With your customReport You'd have to implement IReporter , extend TestListenerAdapter and override generateReport method if you want to implement a custom TestHTMLReporter . For other reporters you may have to do things a bit differently but the concept will remain the same. You'd achieve custom 'TestHTMLReporter' like below .
Create a CustomReport.java file in your project and copy-paste the whole content of TestHTMLReporter.java , change the name of file in getOutputFile method and it would look like below
public class CustomReport extends TestListenerAdapter implements IReporter {
#Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
String outputDirectory) {
}
...
//paste the content of TestHTMLReporter.java here
...
...
Make sure all your imports are in place from TestHTMLReporter.java
Now, in this file change as per your requirement . For ex: if you'd like to add the end time of each of the test then at the correct place in generateTable method add the below snippet
// Test class
String testClass = tr.getTestClass().getName();
long testMillis = tr.getEndMillis();
String testMillisString = Long.toString(testMillis);
if (testClass != null) {
pw.append("<br>").append("Test class Name: ").append(testClass);
// this line to add end time in ms
pw.append("<br>").append("End Time(ms): ").append(testMillisString);
// Test name
String testName = tr.getTestName();
if (testName != null) {
pw.append(" (").append(testName).append(")");
}
Then you'll get like below
Now, You'll get two reports one with default and the other with your file name.
The only thing now remains is switching off the default reporting listeners, so you get only one report. For that you can follow this or you may search for solutions. Hope this helps
I don't want to create a new one or a custom listener. Is that possible? Where is the html report created in TestNG?
SuiteHTMLReporter [source] is the reporter creating the html report. You can extend and override. Disable default listeners and add your own.
I know this is old , but these reports can be edited and custom reports can be made like below. I have explained here how TestHTMLReporter can be edited
With your customReport You'd have to implement IReporter , extend TestListenerAdapter and override generateReport method if you want to implement a custom TestHTMLReporter . For other reporters you may have to do things a bit differently but the concept will remain the same. You'd achieve custom 'TestHTMLReporter' like below .
Create a CustomReport.java file in your project and copy-paste the whole content of TestHTMLReporter.java , change the name of file in getOutputFile method and it would look like below
public class CustomReport extends TestListenerAdapter implements IReporter {
#Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
String outputDirectory) {
}
...
//paste the content of TestHTMLReporter.java here
...
...
Make sure all your imports are in place from TestHTMLReporter.java
Now, in this file change as per your requirement . For ex: if you'd like to add the end time of each of the test then at the correct place in generateTable method add the below snippet
// Test class
String testClass = tr.getTestClass().getName();
long testMillis = tr.getEndMillis();
String testMillisString = Long.toString(testMillis);
if (testClass != null) {
pw.append("<br>").append("Test class Name: ").append(testClass);
// this line to add end time in ms
pw.append("<br>").append("End Time(ms): ").append(testMillisString);
// Test name
String testName = tr.getTestName();
if (testName != null) {
pw.append(" (").append(testName).append(")");
}
Then you'll get like below
Now, You'll get two reports one with default and the other with your file name.
The only thing now remains is switching off the default reporting listeners, so you get only one report. For that you can follow this or you may search for solutions. Hope this helps
Using the Gedmo Sluggable behavior in Symfony2, I need to know if there is a way to allow both slashes (/) and dashes (-) as word separators. In other words, one specific separator as usual, but leaving alone the other specific special character so it is ignored. I want to do something like this as a slug:
products/some-product
This allows me to use slugs in the URL that are categorized via the slash and separate the spaces via the dash. But, for instance, if the separator is "/", the "-" will be replaced as well instead of left alone.
I've looked through the related Sluggable class code (Urlizer) and see a lot of regex going on, but I'm not sure where I should override to allow slashed and/or dashes to NOT be substituted along with everything else.
Turns out you can accomplish this by creating your own class that extends Urlizer, then setting it as the callable for the listener instead of Gedmo's class.
When using STOF doctrine extensions, a sluggable listener is created as a service, but it is set as private so you can't normally access. So you must first create an alias to this listener in your own config:
services:
sluggable.listener:
alias: stof_doctrine_extensions.listener.sluggable
You must then create your class. There is a setter called setTransliterator() that you can use to call your own transliterator, which you can then use to inject what you need to modify the slugging process. The function postProccessText() is what you really want to modify, the transliterate() function is just what is callable:
namespace My\Bundle\Util;
use Gedmo\Sluggable\Util\Urlizer as BaseUrlizer;
class Urlizer extends BaseUrlizer
{
public static function transliterate($text, $separator = '-')
{
// copy the code from the parent here
}
private static function postProcessText($text, $separator)
{
// copy code from parent, but modify the following part:
$text = strtolower(preg_replace('/[^A-Z^a-z^0-9^\/]+/', $separator,
preg_replace('/([a-z\d])([A-Z])/', '\1_\2',
preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2',
preg_replace('/::/', '/', $text)))));
}
}
The regular expressions of postProcessText() are what you want to modify to your liking. After that, you must make your function the callable right before you persist, and you're good to go:
// custom transliterator
$listener = $this->get('sluggable.listener');
$listener->setTransliterator(array('My\Bundle\Util\Urlizer', 'transliterate'));
$em->flush();
I have a model that I am using to present an index of a model from a database and have given a display name to some of the rows that may need spaces in them, (I.e. "weekstarting" in a db would be given a display name of "Week Starting").
So I set the display name for my model like this:
[DisplayName("Week Starting")]
public DateTime WeekStarting { get; set; }
and then in the table headers for my table I use the following line of code to display the field name using its given display name:
#Html.LabelFor(x => x.First().WeekStarting)
The above all works fine. But I am using the W3C validator and it is giving me the following error for the example I have given:
The for attribute of the label element must refer to a form control.
Forgive me if it is obvious but what am I doing wrong here? I am not using a form I am simply displaying an index of items in a table. I have tried to look for an answer and saw someone suggest that the form controls being referred to need ids (even though I'm not using a form) but this would not be applicable in this instance because if I tried to set an id in the index it would be duplicated with each item in the index:
foreach (var item in Model.Tbms)
{
<tr><td>#item.value</td><tr>.... would be repeated for each item, and also unsure where I would put the id in any case, the td?
}
Or is there a better way to label the field header, with my preferred display name in the first place? I guess I could just swap #Html.LabelFor... for Hard code field name but do I have to?
It's inserting a label element, which should correspond to a valid input on the page. You can (a) either not worry about it, (b) output the text directly without using LabelFor, (c) write a custom helper that extracts the label text from the model without wrapping it in a label, or (d) unwrap it with javascript on the client (which will make it pass validation, but only after running the script).
Here's an extension based on the LabelFor code that should help with (c). Untested.
public static class HelperExtensions
{
public static MvcHtmlString TableHeaderFor<TModel,TValue)( this HtmlHelper<TModel> html, Expression<Func<TModel,TValue>> expression )
{
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string headerText = metadata.DisplayName ?? metadata.PropertyName ?? ExpressionHelper.GetExpressionText(expression);
return new MvcHtmlString( headerText );
}
}
Is it just a case issue? Your line:
#Html.LabelFor(x => x.First().Weekstarting)
contains 'Weekstarting', whereas the actual property is called 'WeekStarting'.