I am working on a legacy project based on ZF1 which uses ISO-8859-1 charset. Also the servers default encoding is ISO. New modules should be implemented using ZF2. How can the default encoding e.g. for escapers etc. be set globally to anything else than UTF-8 in ZF2?
If you use escapers directly in your modules, this will be a problem. If you use only the view helpers, there is an option to set the encoding.
Every escaper view helper (EscapeCss, EscapeHtml and so on) extend from the Zend\View\Helper\Escaper\AbstractHelper. This class has a method setEncoding(). because the encoding is not shared between all helper instances, you must set them individually, but you are able to set the encoding there.
For example, you can set the correct encoding during bootstrap. Say you have your Application module:
<?php
namespace Application;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$app = $e->getApplication();
$sm = $app->getServiceManager;
$manager = $sm->get('ViewHelperManager');
$plugins = array(escapehtml', 'escapehtmlattr', 'escapejs', 'escapecss', 'escapeurl');
$encoding = 'ISO-8859-1';
foreach ($plugins as $name) {
$plugin = $manager->get($name);
$plugin->setEncoding($encoding);
}
}
}
This should correct all plugins to the ISO-8859-1 encoding. If any of your modules, or any 3rd party modules, use the escaper view helpers, the ISO-8859-1 encoding will be used.
Related
We are having a project where we are using Xtext to generate a grammar and create from this language a java output file.
Next to that we want to create also a kind of json output file. which has another extension.
For this we want to split the generators to not mix the java generation and the json generation.
Is there anyway we can call 2 Igenerators when compiling dsl grammar?
Example:
We have 1 language common which generates java file for this we have one IGenerator2 which calls like standard CommonGenerator.
Now we want to create second generator for json file with CommonLineageGenerator.
I have read several threads now were i found the following
component = org.eclipse.xtext.generator.GeneratorComponent {
register = CommonStandaloneSetup{}
outlet = {
path = "${runtimeProject}/src-gen"
}
}
component = org.eclipse.xtext.generator.GeneratorComponent {
register = CommonStandaloneSetup2{}
outlet = {
path = "${runtimeProject}/src-gen"
}
}
Where the StandaloneSetup contains an override of the Igenerator2 bindings
return Guice.createInjector(new CommonRuntimeModule())
return Guice.createInjector(new CommonRuntimeModule() {
override Class<? extends IGenerator2> bindIGenerator2() {
return CommonTracingGenerator;
}
});
We are also using mwe2 , to generate our language configuration.
When executing our compilation it now seems although that he is only taking one Generator. Is there anyway we can accomplish this. A wrapper is also possibility but we really want to avoid of mixing the two types of generations.
kr
I am using .LESS variables in my files. I have a LessTransform in my Bundler, which allows all my .less to see the variables. But when I turn bundling off, obviously it no longer works!
Can I see just a single bundle to always be bundled? (even when compilation debug=true)
Unfortunately it's an all or nothing setup (determined very early on by AssetManager.DeterminePathsToRender which, based on EnableOptimizations, either emits a bundle URL or individual script paths).
You could look into using the WebEssentials extension which handles .less (as well as other) files natively. At least then you'll be able to include the compiled version and let you move onto more important matters. Once you've finalized, you can bring bundling back into the equation.
I do not work on/for WebEssentials, I just find the extension very helpful
In the main application that I work with, we use the DotLess compiler directly to serve our stylesheets.
We store custom .LESS variables in the database and combine them with the .less file on the fly.
using System.Web.Mvc;
using dotless.Core;
using System.Web.Helpers;
public class SkinController : Controller
{
private const int TwentyMinutes = 1200;
[OutputCache(Duration = TwentyMinutes, VaryByParam = "*", VaryByContentEncoding = "gzip;deflate", VaryByCustom = "Scheme")]
public ActionResult Index()
{
string variablesFromDatabase = "these came from the database";
string lessFileContents = "this was read from the disk";
string content = Less.Parse(string.Concat(variablesFromDatabase, lessFileContents));
SetEtag(content);
return Content(content, "text/css");
}
private void SetEtag(string content)
{
string acceptEncoding = Request.Headers["Accept-Encoding"];
string value = string.Concat(content, acceptEncoding);
Response.AppendHeader("etag", string.Format("\"{0}\"", Crypto.Hash(value, "md5")));
}
}
I'm using apache velocity in front of LaTeX. The # and $ escape chars are conflicting with LaTeX. I want to replace # with %% and $ with ## to avoid the conflicts. Simply using a string replace on the source file code is not a good solution because I have to use things like #parse and #include. The parsed/included file should also be able to use the modified escape chars. Is there a way to configure this? Is there a configuration option?
You can use a custom resource loader to modify files loaded by #parse:
VelocityEngine engine = new VelocityEngine();
Properties props = new Properties();
props.put("resource.loader", "customloader");
props.put("customloader.resource.loader.class", CustomLoader.class.getName());
engine.init(props);
public static class CustomLoader extends FileResourceLoader {
public InputStream getResourceStream(String arg0) throws ResourceNotFoundException {
InputStream original = super.getResourceStream(arg0);
//TODO modify original, return modified
original.close();
}
}
In Zend framework 1 I can do
try {
$locale = new Zend_Locale('browser');
} catch (Zend_Locale_Exception $e) {
$locale = new Zend_Locale('en');
}
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Locale', $locale);
But how does it work with Zend Framework 2?
I recently blogged about Zend Framework 2 and how all the i18n, l10n and locale settings work. This might be interesting for you, too, as the locale used can be set up by many ways.
Read about it: Zend Framework 2 - translate, i18n, locale
Personally i go with the following approach and then - depending on your structure - you may add locales from either database, session or cookies or whatever ;)
<?php
namespace FileManager;
use Zend\Mvc\ModuleRouteListener;
class Module
{
public function onBootstrap($e)
{
$translator = $e->getApplication()->getServiceManager()->get('translator');
$translator
->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))
->setFallbackLocale('en_US');
}
//...
}
Judging from this RFC, the decision was taken to leave Zend_Locale out of Zend Framework 2 and rely on the core PHP I18n classes.
I would recommend reading the manual starting with the introduction to get a good understanding of the classes and then refactoring your code to use them.
I am trying to override the g:link tag so that I can prefix an extra string. Here is my code:
import org.codehaus.groovy.grails.plugins.web.taglib.*
class ApplicationTagLib {
static namespace = "g"
def link = { attrs, body ->
if("es".equalsIgnoreCase(request.stLocale.language)) {
attrs['controller'] = "es/" + attrs['controller']
}
def applicationTagLib = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
applicationTagLib.link.call(attrs, body)
}
}
This works fine except for when I add "es/" the resulting path gets translated into es%2F instead of es/ which causes the link to not work.
Is there a way to prevent this from automatically encoding the new slash or a better way to prefix this string to the controller path?
You should be aware that in Grails the controller package (thus it's location in the project's structure path) does not correlate with the default URL mapping - the structure is flattened.
The slash you add to the controller name is thus encoded as it would otherwise form a part of the URL (and thus not map to a controller).
Perhaps the logic for handling different locale be better placed in a controller anyway.
You can add this '/es' prefix in all links generated by grails tags by configuring your UrlMappings.groovy. If you're using the default one, generated by grails create-app command, you can add '/es' in your URL's like this:
class UrlMappings {
static mappings = {
"/es/$controller/$action?/$id?" { // <---------- added '/es' prefix
constraints {
// apply constraints here
}
}
"/"(view: "/index")
"500"(view: '/error')
}
}
To learn more about URL Mappings, see the Grails guide.
Regards