I have made a CMS using PHP and MYSQL for the back-end. The problem is, I'd like to have the site available in multiple languages; So I've already translated the CMS itself and put it in files like:
en.ini
nl.ini
Which works fine for the CMS' contents itself, like the Administration etc. The actual problem occurs when I try to translate the website further: The blogposts and pages are stored in a database, so they're dynamic. My pages table's structure looks like this:
urlname | name | id | content | position | hidden | redirect | type | permission
So, if I were to translate a page into, for example, Dutch, I don't want to create a new table called pages_nl for example, because most of the information would be the same as the other table.
I could add a row which contains the page's available languages, and make php read the array and parse if the current $lang matches one of the page's available languages, and then read the content_nl and name_nl row as an example.
The same problem would occur with my blog posts, and I have searched for a solution, but most results were for specific CMSs and just plugins.
I'm asking what would be the best way (and database structure) to store multilingual pages/posts, where languages could be added dynamically and have a fallback on the original content if there's no translated version available.
Basically you add a lang-key to your database
urlname | name | id | content | position | hidden | redirect | type | permission | lang
Now lets say a request comes for a page with 5 content elements in it. Your CMS first search for the entries with the specific lang-code, for example nl. If it dont find an entry with the language code it falls back to the default language and take that entry.
Let say you have translated Content 1, 3 and 5:
Content 1 Content 1 nl
Content 2
Content 3 Content 3 nl
Content 4
Content 5 Content 5 nl
Result:
Content 1 nl
Content 2
Content 3 nl
Content 4
Content 5 nl
Another way would be to do the translation in the template files (like magento does it).
In magento you have something like that:
"Hello, this is the " . $this->__("English") . " page"
And translation files. For example lang.nl:
"English", "Dutch"
In this case you will have only 1 Content Element, but you will wrap every element that shall be translated. This also works with images and everything you want. You can also abstract this wrapper to work in a backend RTE-Editor. For example, you will write Hello, this is the English page in your backend then mark Englishand click the translation button. This will add a Tag around English and when you render the content you scan for this tags and replace it if you find any translation for this string in the requested language.
Related
For some time now I'm thinking about a nice way on how to dynamically load code in php based on database entries. I've tried to look up something related, but couldn't really find anything that answered my question(s) thoroughly. I'm using Laravel - not sure if this might be a subject to solve this particular problem.
See the following code for "example data" where I tried to give a quick overview over the database structure. So for a game, lets say we have characters that can be at a location. Any location basically looks the same. You have a form to write a message at this location - nothing fancy. But then there might be some exceptions. For example you might have a location, that implements some more logic such as listing all online characters (+ showing their current location's name.). Or a location might show some other additional content.
This is what I came up with so far, but neither seems optimal:
Creating different tables for different types of locations (this seems very bad to me actually).
Create another table, f.e. modules, and have a many-to-many relationship with the location table. the modules table would then have entries for a location that tell the application what to "execute". F.e., a location might have the entries thread, for allowing to post messages, list, for showing an overview of online characters and their locations, trader, to allow some gameplay mechanics etc.
To me the second option seems to be kind of the right way of achieving what I want to achieve. I would have classes, functions etc. that would represent these modules. But it too seems very hardcode-y. Meaning, that for one in the code behind I would have to distinct between the values which I might or might not want to change in the future, which then would require a lot of refactoring. Because I have to distinct between strings, this design is prone to typos and what not...
character:
id | location_id (nullable) | name
---------------------------------
1 | 1 | Test
location:
id | name
----------------
1 | Location #1
2 | Online Characters
For the above presented version I would add:
modules:
id | name
---------
1 | thread
2 | list
3 | trader
location_modules:
location_id | modules_id
------------------------
1 | 1
1 | 3
2 | 2
I have a large PHP Document in the following format:
<?php
define("TITLE","HAPPY DAYS");
define("FOOTER","This site is ok");
... 1000 more
?>
I want to translate it to Spanish.
I tried Google translate but it also translated TITLE , FOOTER which are PHP keywords and should be not translated.
Any idea how to automate it?
If my call is
API?text="TEXT HERE"
it returns
TEXT TRANSLATED
I would suggest two ways which i would prefer
Method 1 : ->Manually
You can have a table like this
S.No | Text | French_Text | Spanish_Text
1 | KNowledge | Connaissance | Conocimiento
So You will give the english value by default, then if the user chooses the some language you can set a cookie or some storage locally,
And in the header page according to the selection you will refer the French_Text or Spanish_Text. You can have as many language like this.
Or you can go to any api
Method 2 : -> Using some api
You can select an api which allows to translate with respect to div or class. So that you can have translation for the particular area that you need.
I'm beginning web development and started using PHP and MySQL. I'm trying to make a CMS similar to a technical blog and stuck at database design. Each post can be of any one of the following type.
Rich text - Single part
Rich text - Multiple parts
Video link - Single part
Video links - Multiple parts
Attachment - Single part
Attachments - Multiple parts
(The attachments can be pdf, doc, ppt)
Each post is under section. The sections are stored in a table. Examples for sections are
Tutorial - all types are allowed
Code snippet - only 1 is allowed
Tips or hacks - only 1,2,3,4 are allowed
News update - only 1 and 3 are allowed
Review - only 1 and 3 are allowed
So my question are
How do I store and distinguish single and multipart posts?
What is the feasible/best way to store attachments?
How do I relate sections and posts? ie., How to know/store that a particular section can support post types(all, only 1, only 1 and 2, etc).
Edit:
By single means, I mean 1 post has only 1 part and by multipart I mean 1 post can have several parts
I guess with "single" you mean that one post is related to one section, multipart means several posts relate to one section?
db-design could be...
table section
---------------
id (int, a-i)
type (int) 1=tutorial, 2=code-snippet etc.
name (varchar)
...
table post
---------------
id (int, a-i)
section_id (int) key to section.id
ordering (int) if multi for controlling which part is first, second etc.
type (int) 1=rich-text, 2=video, 3=attachment <---- drop single/multi discrimination here
single (int) 1=yes, 2=no <---- and put it here instead
content (varchar) --> see below
description (varchar) of this post
note: a single is one post, a multi several post linked to the same section.
where to store attachments:
in the file-system of your web server, the db holding the file-name in table.content.
small post-types (e.g. a link) can be stored directly in table.content
--> keeping your db lean and performing
relating sections and posts:
by post.section_id. Like this, you can link 1 or more posts to a section. use post.ordering to put them in the right order.
section
--------------------------
id | 1
name | "How to build a CMS"
type | 1 (tutorial)
post
--------------------------
id | 1
section_id | 1
ordering | 0
type | 1 (rich-text)
content | "/files/130224_1_1_cms.rtf"
...
id | 2
section_id | 1
ordering | 1
type | 3 (video link)
content | "http://www.youtube.com/hgstersh/showid=23jfjr&blabla"
...
which section.type can have which post.types?
on creating a new section, the section.type is set (e.g. drop-down-list in a form)
on creating a new post for this section, the drop-down-list for post-type offers only permitted types based on section.type, this is done in the php-script with if... or switch...case.
If a post can have multiple images, posts, links that's a one-to-many relationship and the typical schema is:
POST: id, title, ...
RICHTEXT: id, post_id, content
LINK: id, post_id, url
If you need a many-to-many relationship, for example a story can have many links but the same link can be on multiple stories you would have:
POST: id, title, ...
LINK: id, url
POSTS_LINKS: id, post_id, link_id
Also take a quick look at schemas for other popular platforms
I am creating a product catalogue which will be browsed through database queries with the results displayed in a div, without page refresh-via ajax.
Category examples would be:
Home
Health
Entertainment
There are also subcategories for each category, i.e:
HOME:
Garden
Furniture
Plumbing
Etc.
I want to make a little directory thing that shows exactly where they are, something like:
Home >>> Garden >>> Lawn care
With each of those as a clickable link to take the person back to that specific query level.
My code is 1 .php document, involving a query and code to output the query. If output is clicked on, it triggers an ajax script which points back to and reruns the same query/output, but with different results.
This being said, i dont know how i would create a way to store and display the directory path. That i mentioned above.
I was thinking of some way that takes the category that was clicked on and passes it through the url so that the php has the value when it reloads. And then i work that variable into a clickable directory link. But the problem is I'm not sure how to do that for multiple layers.
i.e.
If someone just clicked on "garden" i could pass the garden variable through and use that in the nav, but then if someone clicked on "lawn care" i wouldnt be sure how to keep the "garden" variable because the variable i brought over via the url would now read "lawn care."
I feel like it has something to do with dynamically adding and storing the cumulative values in an array, but I'm really out of ideas...
From what you've described, it sounds like you want to implement bread crumbs/categories rather than directories.
If this is the case, you'd basically need to create a Categories table in your database like so:
Categories
id | parent_id | name
1 | 0 | Home
2 | 1 | Garden
3 | 1 | Furniture
4 | 1 | Plumbing
5 | 2 | Lawn Care
This would equate to a hierarchy like the following:
Home
Garden
Lawn Care
Furniture
Plumbing
So if I want to have a product show, for instance, in Home > Garden > Lawn Care, I'll need to link the product to Category #5 (Lawn Care). Then I need to develop a function to do a little while loop that figures out the parent structure from there. It will need to loop until it doesn't find a parent (or until parent_id = 0). In other words, it would go:
I'm in Lawn Care. Does Lawn Care have a parent?
Yes -> Garden. Does Garden have a parent?
Yes -> Home. Does Home have a parent?
No -> End.
There are a number of ways to implement this, which is why I left specifics out.
Alternatively, you could just do this calculation on save of the product or category so it can map out the hierarchy the one time instead of every time (saving on calculations), but this would be a very simple solution that could work for a large number of products.
The benefit to doing it this way is that you can also implement product lists based on the category you're in, and from another perspective, you can create product counts per category.
You can use URLs like \Home\Garden\LawnCare and rewrite them using .htaccess and mod_rewrite.. And when user clicks the Garden or the Home, you can easily go back to the requested page...This is such a simple solution to the problem...
I'm currently have around 100 rows in a table on my website, which include a URL and few sets of numbers pulled from a database on my server. What I would like to do is to dynamically create pages based on a cell of each row, which would contain data pulled from the same database. For example, each row (displayed in the table) would look like this:
Icon (url) | Name (url) | Number 1 | Number 2 | Number 3 | Number 4 | Number 5
Inside my database however, each row is like this:
Icon (url) | Name (url) | Number 1 | Number 2 | Number 3 | Number 4 | Number 5 | Description (large body of text) | LargeImage (url)
Since I have so many entries, I would like to be able to have some way to generate the pages based on the name of the row in the database (it would take too long to make each page individually, and I will be updating this table frequently with content), so I can display more of the information out of the database row (the description, largeimage etc) that I wouldn't be able to fit into the table.
Are there any plugins for Wordpress that can do this, and if not, how would I go about doing this in PHP?
I'm not sure how to best integrate this into WP, but it's fairly straightforward in PHP. You just have a file like mypage.php?id={#} where the # is the individual record's ID. You pull the ID using GET ($id = $_GET["id"];) and then run an SQL query with it as the WHERE, take the results and populate the page with that row of data. Then, using .htaccess, you can do what WP does and make this look like a URL (ie. mypage/2/).
You can create the custom page by using a method like this for example.
You could integrate this into WP by creating a separate file (other than single.php, for example) that would run this PHP script, but include the WP header and footer to make it fit into the theme. However, this wouldn't really be fully integrated into single.php and therefore wouldn't appear in the posts section in the admin or anything. Is that a requirement?