Localize cakephp bake templates - cakephp

I'm creating CakePHP 3 localized app and want to generate all backend part with bake for multiple languages. I'm already familiar with i18n shell and .po files, I have used cakephp/localized and it's ok. It have translations of cake core for many languages.
But one i can't get a glue is how to translate templates generated by bake? It's bunch of similar strings like
The group has been deleted
The user has been deleted
The item has been deleted
and so on. I want to translate all pattern "The {0} has been deleted" once but not for every single entity.
Am i missing something important?

Related

Translating text direcly from DB using .po file

I've been having this problem for several days, after reseaching, I haven't found a solution.
I'm on a cakePHP 2.5.6 project, and I'm on the internationalization right now, everything seemed to work just fine, but I came across this issue:
In my DB I store several tags for my articles, the problem is that the name of the tag is given to me by a query directly from the DB, and printed using a foreach.
Here is my doubt, is there any way to translate those fields using my .po file?
I have thought some solutions but I'm trying to avoid them because I think they are just lame, such as create a new column in my tags table with the translated name in it.
CakePHP's pot files are generated for hard-coded strings in template files and elsewhere in your app. Basically anything like __('Hello'), it doesn't work if you use variables like __($var) as it needs to know the string to generate the translation files.
If the content is coming from the database you need to look at translating the database content within the database as you've suggested. Rather than create a new set of columns on your tags table you should look into using CakePHP's Translate behaviour. Cake's Translate behaviour should do what you are looking for without you needing to re-invent the wheel.

cakephp - naming tables and using cake bake

I'm having some weird behaviour from the cake console and I can't figure out what could possibly be wrong!
I've used the cake bake console before and not had any problems so hopefully it's not something glaringly stupid.
I have the tables: users, images and folders.
Folders and images both have user_id's
When I run the cake bake all on the folders table it bakes the controller and model but there's errors baking the views. I figured perhaps you can't call a table folders because of conventions or whatever so I renamed it 'archives'.
Now when I run the console and cd to myApp/app and cake bake all I get the model:
1 archife
2..
archife??
My table is most definitely called archives. I've changed the name a bunch of times to other names to check and it only seems to be on the particular word archives which it turns into archife. I've checked other words with 'v' in it but they're fine.
So.. my questions are twofold. Can we not use the table name folders because there's some problem with it? It's the only name I've come across apart from pages which you can't use as a table name because it clashes.
And, why does cake bake the table archives to the model archife?
Model names should be unique...
As a general rule, don't use names for your tables that would result in a model class name that equals an existing class name, Folder for example is a utility class shipping with CakePHP, and this will cause problems with autoloading as you've experienced.
This is a problem that should/will be fixed with CakePHP 3, where namespaces are being used.
...and singular
archives being turned into archife sounds like it could be an Inflector bug/conflict, try
debug(Inflector::singularize('archives'));
It's working fine for me using CakePHP 2.5.1, ie it returns archive as expected, so I'd suggest to upgrade your CakePHP version, and report it as a bug in case it's happening with a recent version.

Share controllers, models and views in multisite CakePHP 2.3 installation

I'm creating a CakePHP 2.3 advanced installation (several apps -websites- that share one same lib folder where all of cake's core files are located). This works without any problems, I just edit the core.php file in the Config folder for each app so it knows where to find cake's files. The file system looks something like:
[root]
[cake-core-files]
[websites]
[website-1]
[app]
[plugins]
[vendors]
[website-2]
...
[website-N]
These different apps are in fact different in some things (they are different websites) but at the same time there's many things that are common to all of them (for example some models, controllers, functions...). What I would like to do, if possible, is to have those apps also share a bunch of controllers, models, etc so I can put them in one place, instead of replicating them now for each app.
I've seen the concept of vendors and plugins in CakePHP (I actually use plugins in those websites, but from the /app/plugins folder), but I'm not sure if that would work in my case, or how I would set that up. I guess the idea would be to have another folder (for example [shared_objects]) at the same level of [cake-core-files] and [websites], but I don't know how I would have to configure cake to do that or how to call those objects from each app.
Any ideas?
EDIT
Based on the comments/responses below I'm trying to do this using the App:build() function in the bootstrap.php, but I can't get it to work. This is what I've done:
Added a new folder where I want to put the stuff to share between all apps:
[root]
[cake-core-files]
[shared-stuff] --> NEW FOLDER
[Model]
[Config]
[websites]
[website-1]
etc...
Placed the model User.php inside the new folder [shared-stuff/Model]
Added this line in the bootstrap:
App::build(array('Model' => array('/path/to/shared-stuff/Model')));
Deleted the model User.php from its original location [website-1/app/Model]
After this steps, it seems to work, the model User.php is loaded correctly from the [shared-stuff] folder (I've tested that printing App::objects('Model');, the User model is listed). However, it actually doesn't work, when I call that model from the UsersController in the login() function, the login doesn't work (although I don't receive any kind of error, even with debug set to 2).
This model uses a database configuration different from the default one (the users table is located in a different database than the default one). I don't know if this matters.
One thing is for sure, if I leave the same exact User.php model in its original location ( [website-1/app/Model]) it all works fine, including the login, so it's a problem with how I try to configure all this sharing stuff, not the model itself.
Any ideas what am I doing wrong?
I think it could be useful to share some controller/model between multiple websites, and do it without a plugin: Using shared controller/model lets you to overwrite it if needed. It should happen simply copying the controller/model in the website's correct folder and the system should use it instead of the shared one!
EDIT: Wonderful, it works, but i think there is a little error in cake's official documentation: All paths should be terminated with a Directory separator! In the cakephp book there aren't trailing slash. Add it to your App::build path and everything will work perfectly!
You can have plugins in the core Plugins/ dir
[root]
[lib]
[Cake]
[Plugins]
[Available]
[To]
[All]
[website-1] // the 'app' dir -> https://github.com/cakephp/cakephp/tree/master/app
[plugins]
[vendors]
[website-2] // you can have many of them named anything, 'app' is just one.
...
[website-N]
This folder specifically will make the plugins available to any app using the cake lib
EG:
Look at this. Copy app to website-1, repeat till website-n.

cakephp finding multiple images and displaying them (possibly outside webroot?)

I am currently trying to build a cakephp app that will list items from a database, and there is a field in the database named picture. This picture field contains a string which is the primary picture, so for example ABCD, and then in the images folder this is ABCD.jpg
However there is usually various extra pictures for this particular item, which may be named ABCD1 or ABCD_2 or some other such variating suffix. I had previously written some script in php which used a glob function and counted the files and outputted them.
I am seriously struggling to do this in cakephp, at the moment there is several thousand images stored in the webroot of the server in /images and I understand that cake will be looking in the webroot of the app like /app/webroot, so is it possible to view their original location? or must they be moved...
I have read a little about the file and folder classes but I am struggling to comprehend them, not sure if my brains failed me here or if this is actually a difficult concept..
my question is how would I find any files that begin with this prefix in the picture field after selecting by the id of the item, to then display all those that match for that particular item?
thanks for your help!
Not a real answer to your question but you should start making a database relationship with your pics.
Check out hasMany to make a relationship between your Article (or whatever it is) and the Picture models. Now you will not have to look for each filename, as you'll have it in your database.

Using database for Internationalization & Localization

The CakePHP book has a section on Internationalization & Localization. Bascially what it says is Cake will generate a pot file and we have to create a .po for different language file from the pot file.
My problem is I want the user to be able to edit these localized text so instead of storing the localized text in the .po file I want to store them in the database.
So how can I store the localized text in the database?
There's a Behavior for that.
Personally I find the TranslateBehavior to be rather inflexible though, so I usually just go with a separate table connected to the main model via a belongsTo relationship which contains all the translations. I then pick the one to display in the View.

Resources