How to implement Zend Translation in Core PHP Project - php

I need to implement Zend Translation in a project that is made in Core PHP that is i don't want to use Full ZF Just Zend Translation Classes that is used for Translation.
How would i do that means how would i start ?
which classes i need to add ??
i need the Flow that how would i do this ??

You need to copy these files from Zend Library
Zend_Translate.php and Zend_Translate (directory) everything else is in documentation.
You are wrong in thinking that docs says about implementation in ZF.
Here is example page:
First code snippet from above page:
try {
$translate = new Zend_Translate(
array(
'adapter' => 'Company_Translate_Adapter_MyFormat',
'content' => '/path/to/translate.xx',
'locale' => 'en',
'myoption' => 'myvalue'
)
);
} catch (Exception $e) {
// File not found, no adapter class...
// General failure
}
As you see, there is only Zend_Translate class used.
All zend library is developed to be quite standalone, which means that you can separate classes by theirs functionalities (you can do it by copy files using class names). More informations you can find in documentation (as always ;))

Related

Can I use the Blade templating engine outside of Laravel?

i'm want to creating a design pattern and use the "Blade templating engine".
Can I use the Blade templating engine outside of Laravel and use it in my new pattern ?
For the record:
I tested many libraries to run blade outside Laravel (that i don't use) and most are poor hacks of the original library that simply copied and pasted the code and removed some dependencies yet it retains a lot of dependencies of Laravel.
So I created (for a project) an alternative for blade that its free (MIT license, i.e. close source/private code is OK) in a single file and without a single dependency of an external library. You could download the class and start using it, or you could install via composer.
https://github.com/EFTEC/BladeOne
https://packagist.org/packages/eftec/bladeone
It's 100% compatible without the Laravel's own features (extensions).
How it works:
<?php
include "lib/BladeOne/BladeOne.php";
use eftec\bladeone;
$views = __DIR__ . '/views'; // folder where is located the templates
$compiledFolder = __DIR__ . '/compiled';
$blade=new bladeone\BladeOne($views,$compiledFolder);
echo $blade->run("Test.hello", ["name" => "hola mundo"]);
?>
Another alternative is to use twig but I tested it and I don't like it. I like the syntax of Laravel that its close to ASP.NET MVC Razor.
Edit: To this date (July 2018), it's practically the only template system that supports the new features of Blade 5.6 without Laravel. ;-)
You certainly can, there are lots of standalone blade options on packagist, as long as you are comfortable with composer then there should be no issue, this one looks pretty interesting due to having a really high percentage of stars compared to downloads.
Be warned though i have not tried it myself, like you i was looking for a standalone option for my own project and came across it, i will be giving it a real good workout though at sometime in the near future,
Matt Stauffer has created a whole repository showing you how you can use various Illuminate components directly outside of Laravel. I would recommend following his example and looking at his source code.
https://github.com/mattstauffer/Torch
Here is the index.php of using Laravel Views outside of Laravel
https://github.com/mattstauffer/Torch/blob/master/components/view/index.php
You can write a custom wrapper around it so that you can call it like Laravel
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;
function view($viewName, $templateData)
{
// Configuration
// Note that you can set several directories where your templates are located
$pathsToTemplates = [__DIR__ . '/templates'];
$pathToCompiledTemplates = __DIR__ . '/compiled';
// Dependencies
$filesystem = new Filesystem;
$eventDispatcher = new Dispatcher(new Container);
// Create View Factory capable of rendering PHP and Blade templates
$viewResolver = new EngineResolver;
$bladeCompiler = new BladeCompiler($filesystem, $pathToCompiledTemplates);
$viewResolver->register('blade', function () use ($bladeCompiler) {
return new CompilerEngine($bladeCompiler);
});
$viewResolver->register('php', function () {
return new PhpEngine;
});
$viewFinder = new FileViewFinder($filesystem, $pathsToTemplates);
$viewFactory = new Factory($viewResolver, $viewFinder, $eventDispatcher);
// Render template
return $viewFactory->make($viewName, $templateData)->render();
}
You can then call this using the following
view('view.name', ['title' => 'Title', 'text' => 'This is text']);
Yes you can use it where ever you like. Just install one of the the many packages available on composer for it.
If you're interested in integrating it with codeigniter I have a blog post here outlining the process.
Following the above steps should make it obvious how to include it into any framework.

Drupal 8 custom module add php classes

I have created a custom Drupal 8 module that works as is with a custom block and block form to collect some info.
This is all good.
I also have a twig template that I want to render a twitter feed using a php feed class I bought. I just don't know how it integrate this into the module.
This is the setup for the class: http://austinbrunkhorst.com/demos/twitter-class/#setup
It contains two files:
ultimate.twitter.feed.php
and
tmhOAuth.php
Which is currently a require_once 'tmhOAuth.php'; in the head of ultimate.twitter.feed.php
According to the instruction I should be creating a php file that has this:
$options = array(
'screen_name' => 'FeedTestUser',
'consumer_key' => '...',
'consumer_secret' => '...',
'user_token' => '...',
'user_secret' => '...',
);
$twitter = new Twitter($options);
$twitter->PrintFeed();
Which I'm guessing is also a hurdle as twig files are not php
Any help with this is very much appreciated.
C
I would setup the class as a Service in your module. Your block will then implement that service and do the handling. You don't really want to use require_once() if you can avoid it, rather use Drupal constructs (in part so that if you reorganize things later Drupal will help find the files in their new location).
Place the class in your module's src directory, and add a namespace to the start of the file (assuming there isn't one there already). Then in your block's class file you should be able to add a use statement that references that name space (even better would be to use a dependency injection, but the details on that would get in your way here).
In your block class's build() method you then instantiate the class as described in your question, but instead of just letting the module print HTML, you can want to capture that HTML and place it into your block as markup. If the class allows you to do that without using a buffer, you should (but I didn't see anything in the docs to support that), and then attempt to theme the structured data. If not, you can use PHP's output buffering to capture its attempt to print:
ob_start();
$twitter->PrintFeed();
$content= ob_get_contents();
ob_end_clean();
Then place the generated markup into a render array:
return [
'my_twitter_block' => [
'#markup' => $content,
],
];
Create a custom block and add the result of PrintFeed() to the render array. Just as with any usual custom block. In the render array you can specify a template which should be used (if needed). If you wanna output pure html without any template you could use the '#markup' key.
Small example:
Your block render array:
return array(
'#theme' => 'name_of_your_theme',
'#some_twig_variable' => $twitter->PrintFeed();
);
your your_module.module file (in the root of your module folder):
function your_module_theme() {
return array(
'name_of_your_theme' => array(
'variables' => array(
'some_twig_variable' => some-default-value,
),
),
);
}
your name-of-your-theme.html.twig template (should be under your_module/templates):
{{ some_twig_variable }}
As far as using the class: I see no problem using a require_once for that matter (in your Block php file). Of course it's always better/nicer if you can require the library/package via the make file or composer and then use the autoloader, but if that's not possible just put it e.g. in your drupal root under /libraries/twitter or so and then require it. If you do it like that you have to check that library into your git repository obviously.
have you use ultimate.twitter.feed.php in your TwitterBlock.php file
If not then try adding this line before class block beginns:
require_once 'path/to/twitter_class/ultimate.twitter.feed.php';

Alternative php silex template

Lately I've been using a template manager for silex, but I've noticed that it's been abandoned and no longer works for newer versions of silex. Here's an example code of what it does (it can be seen on its GitHub page):
<?php
use Herrera\Template\TemplateServiceProvider;
use Silex\Application;
$app = new Application();
$app->register(new TemplateServiceProvider(), array(
'template.dir' => '/path/to/dir',
'template.dir' => array(
'/path/to/dir1',
'/path/to/dir2',
'/path/to/dir3',
)
));
$app['template.engine']->render('test.php');
So, what it exactly does is that it renders the file you give, and you can also give other parameters to send it to the file before rendering it... It was very useful to me, but as I said, it's been abandoned and it no longer works with newer versions of Silex.
So, what I'm asking is: is it a good alternative of this that works with newer versions? Should I downgrade my Silex in order to be able to use this? Or is it very hard to try to "create" a system for being able to use this?
I've heard about Twig, but it doesn't really convince me because it doesn't seem to be convenient with what I want to achieve.
Thanks!
You can have the same result just extending the twig loader (Twig_Loader_Filesystem).
$app['twig.loader.filesystem'] = $app->share(
$app->extend('twig.loader.filesystem', function($loader, $app) {
$loader->addPath('/path/to/dir1');
$loader->addPath('/path/to/dir2');
$loader->addPath('/path/to/dir3');
return $loader;
}
);
Then you just use twig as always. $app['twig']->render('template.twig', array(...));
The filesystem loader will look for templates in /path/to/dir1, and if they dont exists it will fallback to look for them in /path/to/dir2 and so on.
If you insist in using a pure PHP templating engine you can do it with the Symfony Templating Component:
Install with composer symfony/templating and then register the service:
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Component\Templating\Loader\FilesystemLoader;
$app['templating'] = $app->share(function() {
$loader = new FilesystemLoader(array(
'/path/to/dir1',
'/path/to/dir2',
'/path/to/dir3',
));
$nameParser = new TemplateNameParser();
$templating = new PhpEngine($nameParser, $loader);
return $templating;
});
Then you just use this engine as $app['templating']->render('template.php', array(...));

Adding a version parameter to YII for application version

We are building a web application using Yii as the framework. Where would be a good location for us to put a version information array?
This version array is not the version of Yii but the version our application is on. This way we can use it global throughout the application. Example when deploy the application on our servers we can have a conditional that compares the required_php_version against the server's php version (phpversion()) to throw errors. This is just a simple example.
The array would consist of (with possibility to evolve later):
<?php
array(
'version' => '2.0.1',
'required_php_version' => '5.4.4'
);
?>
As far as I know, The best place to put your configurations in an application based on Yii, is main.php config file, which is situated in protected/config/main.php. But it is important to put your custom configurations in a right place. That is in params array. You can put your configs like below in config file:
'params' => array(
'webmaster' => 'YourEmail#example.com',
'required_php_version' => '5.4.1',
'my_app_version'=>'2.0.1.1',
'info_in_array'=>array(1,2,3,4,'so on ...')
// and so on
),
You can use these information in everywhere of your application like below:
Yii::app()->params['required_php_version'] //which returns 5.4.1 in this example.

how we could create translate validate error messages on zend framework?

how we could create translate validate error messages on zend framework?
someone could give a example ?
thanks
From the ZF Manual on Zend_Validate Validation Messages
$validator = new Zend_Validate_GreaterThan();
$validator->setMessage('Please enter a lower value',
Zend_Validate_GreaterThan::NOT_GREATER);
And also:
Zend Framework is shipped with more than 45 different validators with more than 200 failure messages. It can be a tendious task to translate all of these messages. But for your convinience Zend Framework comes with already pre-translated validation messages. You can find them within the path /resources/languages in your Zend Framework installation.
[...]
So to translate all validation messages to german for example, all you have to do is to attach a translator to Zend_Validate using these resource files.
$translator = new Zend_Translate(
'array',
'/resources/languages',
$language,
array('scan' => Zend_Locale::LOCALE_DIRECTORY)
);
Zend_Validate_Abstract::setDefaultTranslator($translator);
Of course, you can also provide your own translations. All you have to do is load make them available to the translation adapter. Basically you just swap out the part shown above to your custom path.
I just want to improve a little bit the answer from Gordon:
a working example is
$translator = new Zend_Translate(
'array',
'resources/languages', // you need to copy the resources folder
// (from your Zend Framework installation)
// in the application folder
'it', // 'it' for italian, 'fr' for french, etc.
// Just look at the directories
// Zend_Translate, NOT Zend_Locale
array(
'scan' => Zend_Translate::LOCALE_DIRECTORY
)
);
Zend_Validate_Abstract::setDefaultTranslator($translator);
Cheers!
Bruno

Categories