I'm trying to add localization support for a card built for the Laravel Nova dashboard.
I already created a folder in /resources/lang containing the JSON language files in a format like en.json. The files get published (copied) with the publish command but the loadJsonTranslationsFrom() does not seem to do anything:
class CardServiceProvider extends ServiceProvider
{
public function boot()
{
$this->publishes(
[__DIR__ . '/../resources/lang' => resource_path('lang/vendor/my-custom-card')],
'my-custom-card-lang'
);
$this->loadJsonTranslationsFrom(resource_path('lang/vendor/my-custom-card'));
}
}
This is how the markup in Card.vue looks like:
{{__('Title')}}
How can I test if the JSON files are loaded correctly? What am I missing?
The question is how do I support localization for cards in Laravel Nova?
Card localization has been solved in Laravel Nova 2.
To localize strings use the __ helper within your Vue components and load the according translation files within your NovaServiceProvider:
Nova::serving(function (ServingNova $event) {
Nova::script('{{ component }}', __DIR__.'/../dist/js/card.js');
Nova::style('{{ component }}', __DIR__.'/../dist/css/card.css');
Nova::translations(__DIR__.'/../resources/lang/en/card.json');
});
An exemplary implementation can be found on GitHub.
Further information is now available in the documentation.
I'm having the same issue, but for a tool, also in Nova 2.0.
I found a somewhat elegant solution - maybe it helps someone nonetheless.
Create en.json in /nova-components/{your-tool}/resources/lang/
In /nova-components/{your-tool}/resources/js/tool.js add Vue.mixin(require('./translation'));.
It should look something like this:
Nova.booting((Vue, router, store) => {
router.addRoutes([
{your-routes}
]);
Vue.mixin(require('./translation')); <-------------- add this line!
});
Create the /nova-components/{your-tool}/resources/js/translation.js, it should look like this:
module.exports = {
methods: {
__(key, replace) {
var translations = _.merge(window.config.translations, window.config['tool-translations']);
var translation = translations[key]
? translations[key]
: key;
_.forEach(replace, (value, key) => {
translation = translation.replace(':' + key, value)
});
return translation;
}
}
};
Now you have to add the following to the Nova::serving() function inside the boot() function of your /nova-components/{your-tool}/src/ToolServicePrivoder.php file:
Nova::provideToScript([
'tool-translations' => $this->getTranslations(),
]);
Now add below said boot() function the following:
private static function getTranslations()
{
$translationFile = __DIR__ . '/../resources/lang/' . app()->getLocale() . '.json';
if (!is_readable($translationFile)) {
return [];
}
return json_decode(file_get_contents($translationFile), true);
}
Related
I'm creating a Tool for Laravel Nova 2.0
In my Tool I want to send a list of stuff to the Vue component:
in the PHP src for my tool I have a function that generates the "meta", as suggested in the documentation here:
public function stuff() {
$stuff = [];
...
return $this->withMeta(['stuff' => $stuff]);
}
In my NovaServiceProvider.php I instantiate the tool and call the meta function. i.e.
public function tools()
{
return [
(new Tool())->stuff(),
];
}
However, nothing is passed to the Tool.vue component, (I have spent sometime inspecting it!) i.e.:
mounted() {
console.log(this.stuff); // undefined
},
Issue is discussed here: https://github.com/laravel/nova-issues/issues/761, however note that I am using a Tool and not a ResourceTool, or a Card.
Is this a bug with Tools, or something I'm doing wrong? Is there a workaround?
I haven't tried creating a custom tool yet, but for the custom field you can get the metadata using:
mounted() {
console.log(this.field.stuff);
},
if you're having troubles with similar stuff, i suggest printing the content of the class in the console in a json format, it makes it easier for you to troubleshoot the problem
mounted() {
console.log(JSON.stringify(this));
},
Although i don't think this is the problem can you try modifying your stuff() function to:
public function stuff()
{
$stuff = [];
$this->withMeta(['stuff' => $stuff]);
return $this;
}
Using Swashbuckle in conjuntion with c.MultipleApiVersions((apiDesc, version) =>... the result is our swagger file resides at eg: https://host/api/swagger/docs/{version}. I would like to actually have the swagger file at https://host/api/{version}/swagger. Is it possible the I can set this up in my SwaggerConfig .EnableSwagger()?
This would allow for the following Urls :
http://host/api/v1/swagger/
http://host/api/v2/swagger/
Appreciate the help.
To do that way, you can update the swaggerconfig file as shown below:
.EnableSwagger("{apiVersion}/swagger", c =>
{
c.MultipleApiVersions(
(vc) =>
{
vc.Version("v2", "Swashbuckle Dummy API V2");
vc.Version("v1", "Swashbuckle Dummy API V1");
});
});
Just for further references in asp.net core 2.2 it will lokk like this
app.UseSwagger(options =>
{
options.RouteTemplate = "docs/{documentName}/docs.json";
});
app.UseSwaggerUI(options =>
{
//Build a swagger endpoint for each discovered API version
foreach (var description in provider.ApiVersionDescriptions.OrderByDescending(x=>x.ApiVersion).AsList())
{
options.SwaggerEndpoint($"/docs/{description.GroupName}/docs.json", description.GroupName);
}
options.RoutePrefix = "docs";
}
);
in which provider is IApiVersionDescriptionProvider injected by DI
I have a class that sets up my smarty instances:
class View {
protected $templateEngine;
protected $templateExtension = '.tpl';
public function __construct(){
global $ABS_PUBLIC_PATH;
global $ABS_PUBLIC_URL;
$this->templateEngine = new Smarty();
$this->templateEngine->error_reporting = E_ALL & ~E_NOTICE;
$this->templateEngine->setTemplateDir($ABS_PUBLIC_PATH . '/templates/');
$this->templateEngine->setCompileDir($ABS_PUBLIC_PATH . '/templates_c/');
$this->templateEngine->assign('ABS_PUBLIC_URL', $ABS_PUBLIC_URL);
if(isset($_SESSION['loggedIn'])){
$this->assign('session', $_SESSION);
}
}
public function assign($key, $value){
$this->templateEngine->assign($key, $value);
}
public function display($templateName){
$this->templateEngine->display($templateName . $this->templateExtension);
}
public function fetch($templateName){
$this->templateEngine->fetch($templateName . $this->templateExtension);
}
}
Then in my functions, I use the class like this:
public function showMeSomething()
{
$view = new View();
$view->assign('session', $_SESSION);
$view->display('header');
$view->display('index');
$view->display('footer');
}
Now, I'm trying to fetch some data into a variable, in order to send emails from my template files as well. Unfortunately, this var_dumps below (both of them) output NULL - even though the template file referenced has a lot of HTML in it. Furthermore, changing the word fetch to display below will correctly display the template file. So, the problem certainly lies within the fetch command. I'm not sure what to do to continue debugging.
function emailPrep($data,){
$mailView = new View();
$emailHTML = $mailView->fetch('myEmail');
var_dump($mailView->fetch("myEmail"));
var_dump($emailHTML);
}
Your code must be
public function fetch($templateName){
return $this->templateEngine->fetch($templateName . $this->templateExtension);
}
In .NET MVC there is #Url.Action() and in RoR there is url_for()
I could not find similar url building helper in angularjs.
I'm already providing everything that is needed to build url to $routeProvider so something like: $routeProvider.urlFor("MyCtrl", {id: 5}) could be nice to have.
My main goal here is to avoid hardcoded urls in viewes and other places and to avoid repeating url/routes patterns twice.
UPDATE:
Seems like it's hard to explain what i want so here is exact example of what i want:
Instead of writing this in viewes:
<a ng-href="/product/5">foo</a>
I want to write this:
<a ng-href="urlFor("ProductCtrl", {id:5})">foo</a>
So if later i decide to change path of ProductCtrl I would not have to update url in this a element.
What would be good solution for my goals?
You could try with something like the following (just came up with it) inside your main module's run() block:
app.run(function($route, $rootScope)
{
$rootScope.path = function(controller, params)
{
// Iterate over all available routes
for(var path in $route.routes)
{
var pathController = $route.routes[path].controller;
if(pathController == controller) // Route found
{
var result = path;
// Construct the path with given parameters in it
for(var param in params)
{
result = result.replace(':' + param, params[param]);
}
return result;
}
}
// No such controller in route definitions
return undefined;
};
});
This will extend the root scope of the application with the new path() function - so it can be used anywhere in the application. It uses the $route service to get the controller names and the corresponding paths, so you won't have to repeat yourself.
Example usage:
{{ path('LibraryController', { bookId : 'x', chapterId : 'y' }) }}
Live example at: http://plnkr.co/edit/54DfhPK2ZDr9keCZFPhk?p=preview
There are numerous approaches...a custom directive or ng-click to modify $location, or using a function in ng-href to parse the url from object and have it placed as href in an <a> tag.
Example using ng-href:
HTML:
<li ng-repeat="item in items">
<a ng-href="{{url(item)}}">{{item.txt}}</a>
</li>
JS:
function Ctrl($scope){
$scope.items=[
{id:1,txt:'foo'},
{id:2,txt:'bar'}
];
$scope.url=function(item){
return '#/'+item.id
}
}
Example using ng-click and $location
HTML:
<a ng-click="newPath(item)">{{item.txt}}</a>
JS:
function Ctrl($scope){
$scope.items=[
{id:1,txt:'foo'},
{id:2,txt:'bar'}
];
$scope.newPath=function(item){
$location.path('/'+item.id)
}
}
DEMO: http://jsbin.com/ovemaq/3
In one of recent project I came up to this solution it helped me to solve my needs right in time. It will be interesting to help you to improve it to fit your usecase.
1. Move routes definition to config:
...
ROUTES: {
PAGE1: '/page1/:id',
PAGE2: '/page2/:id/:name'
},
...
2. Define routes, using values from config:
app.config(['$routeProvider', 'config', function ($routeProvider, config) {
$routeProvider.when('/', {
templateUrl: 'partials/home.html',
controller: 'HomeCtrl'
});
$routeProvider.when(config.ROUTES.PAGE1, {
templateUrl: 'partials/page1.html',
controller: 'PageOneCtrl'
});
...
$routeProvider.otherwise({
redirectTo: '/'
});
}]);
3. Set up service to provide functionality to create urls:
services.factory('routes', function (config) {
// populates `:param` placeholder in string with hash value
var parseString = function (string, parameters) {
if (!string) return '';
if (!parameters) return string;
for (var index in parameters) {
if (!parameters.hasOwnProperty(index)) continue;
string = string.replace(':' + index, parameters[index]);
}
return string;
};
return {
getPage1Link: function (urlParams) {
return '#' + parseString(config.ROUTES.PAGE1, urlParams);
}
};
});
== Drawbacks ==
With this approach I had to define getter for each route (there were less then 5, and development speed was vital)
It appears that what you want is the ui-route directive which has just been recently added to the angularui project. There is a nice set of out-of-the-box options.
In symfony, I call an action and I want this to return json to jQuery frontend.
The Jobeet tutorial teaches how to return a partial but I want to return json, not a partial.
If it's just a normal AJAX action you're returning it from, I think I've used the following somewhere in the past:
return $this->renderText(json_encode($something));
The cheap way:
function executeSomethingThatReturnsJson(){
$M = new Model();
$stuff = $M->getStuff();
echo json_encode($stuff);
die(); //don't do any view stuff
}
The smarter way:
A smarter way is to create a nice subclass of sfActions that helps handling json-stuff.
In a project I did recently, I created a application called 'api' (./symfony generate:application api)
and then created a file like:
api/lib/apiActions.class.php
<?PHP
class apiActions extends sfActions {
public function returnJson($data){
$this->data = $data;
if (sfConfig::get('sf_environment') == 'dev' && !$this->getRequest()->isXmlHttpRequest()){
$this->setLayout('json_debug');
$this->setTemplate('json_debug','main');
}else{
$this->getResponse()->setHttpHeader('Content-type','application/json');
$this->setLayout('json');
$this->setTemplate('json','main');
}
}
}
Notice that I explicitly set the template there.
So my jsonSuccess.php template is simply:
<?PHP echo json_encode($data);
While json_debugSuccess.php makes things prettier:
<?PHP var_dump($data); ?>
Then you can have a controller that extends apiActions (instead of the usual sfActions) that looks like this:
<?php
class myActions extends apiAction {
public function executeList(sfWebRequest $request)
{
$params = array();
if ($request->hasParameter('id')){
$id = $request->getParameter('id');
if (is_numeric($id)){
$params['id'] = $id;
}
}
$data = Doctrine::getTable('SomeTable')->findAll();
$this->returnJson($data);
}
}
Disclaimer: The code above is copy/pasted out of an app I have, but simplified. It's for illustrative purposes only -- but it should get you heading in the right direction.
FYI: In case of Symfony 2.x "quick and dirty" way looks like this:
return new Response(json_encode($data), 200, array('Content-Type', 'text/json'));
Return new JsonResponse(array);