Post by yoshiro » Sun Nov 17, 2013 11:39 pm

Hey All,

I have some knowledge in PHP, OOP and MVC but I really want to learn more and I think that learning how Opencart is structured and operates would teach me a lot on the subject.

I currently have a simple task for my own store, I want to add a translation layer for Countries / Zones and maybe later on for Cities as well. I saw that there are already extensions that do that out there. But I really want to make my own so I can learn something on the way.

My Idea basically is to add an intermediate layer that will interact as a translator to whatever data I fetched.
so for example let's say the I want to translate a country name the pseudo code would be:

1. get user current language
2. fetch country id x
3. fetch translation from (JSON File/DB whatever) where lang= user_lang and country_id=x

and of course the reverse when editing translations from the admin panel.

It looks simple but I really don't know where to start and how to plug it into Opencart so that I don't break anything. I already used vQmod for a little so I know how to change files without really screwing them up.

If I'm correct I need first to take care of the admin part of the extension and set up the form that from there I could edit and add translations ?

I need to find all places that country names and/or zones are fetched and implement my code. right ?
Can I use JSON in order to keep translations per language ?

I appreciate your help

Newbie

Posts

Joined
Wed Nov 13, 2013 12:01 am

Post by butte » Mon Nov 18, 2013 1:38 am

Start with http://forum.opencart.com/viewtopic.php?f=131&t=47800 and read the whole thread.

Guru Member

Posts

Joined
Wed Mar 20, 2013 6:58 am

Post by yoshiro » Mon Nov 18, 2013 5:06 am

I read it all , and I have to say most of the thread was about problems and bugs related to vQgenerator and aren't related to the main issue of my post. Anyhow I'm ready for the next step.

Newbie

Posts

Joined
Wed Nov 13, 2013 12:01 am

Post by butte » Mon Nov 18, 2013 5:51 am

I just wanted for you to see his generator and its own installable alternatives . . . in what you want to do, it might come in handy.

Guru Member

Posts

Joined
Wed Mar 20, 2013 6:58 am

Post by yoshiro » Tue Nov 19, 2013 1:26 am

Ok Thanks, I made some progress today already used vQmod to edit the controller/localisation/country.php and view/template/localisation/country_form.tpl

I added access to languages within the controller country.php by adding this lines:

Code: Select all

$this->load->model('localisation/language');
		
$this->data['languages'] = $this->model_localisation_language->getLanguages();
I added a loop in the country_form.tpl that loops through all the languages (minus english I left the old name input as is) and adds an <Input> for translation followed by the language flag by changing this code:

Code: Select all

<td><span class="required">*</span> <?php echo $entry_name; ?></td>
<td><input type="text" name="name" value="<?php echo $name; ?>" />
              <?php if ($error_name) { ?>
              <span class="error"><?php echo $error_name; ?></span>
              <?php } ?>
</td>
Into this:

Code: Select all

 
<td><?php foreach ($languages as $language) { ?>
          <?php if ($language['language_id'] != 1 ) { ?>
                 <input type="text" name="[<?php echo $language['name']; ?>][<?php echo $language['language_id']; ?>]" value="" />
         <?php } else { ?>
                    <input type="text" name="name" value="<?php echo $name; ?>" />
         <?php } ?>
         <img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /><br />
         <?php if ($error_name) { ?>
                     <span class="error"><?php echo $error_name; ?></span>
         <?php } ?>
         <?php } ?>
</td>
Basically asking if language is not 1 (english) add a new input line for translation. (btw I hardcoded the number 1 cause I wasn't sure from where to pull the admin default language -> help :)

so far it looks pretty much as I wanted:
Image

Now comes the part where I'm getting a little confused ... I need to store the data somehow and to make it CRUD as it should with the country.
How should I do it ? The straight thinking tells me to create a table that holds translations and to add a seperate model file with the CRUD for the translation and call it from the country controller on insert / update / delete moethods.

But, I think there must be a smarter way to go about it ... need your advise !

Newbie

Posts

Joined
Wed Nov 13, 2013 12:01 am

Post by pablofiumara » Tue Nov 19, 2013 5:13 am

yoshiro,

You have already modified the controller and the view. It seems you only need to modify the model (adding your changes).

May I ask you what CRUD is?

Thanks

User avatar
New member

Posts

Joined
Sun Nov 10, 2013 6:29 am


Post by Qphoria » Tue Nov 19, 2013 5:39 am

pablofiumara wrote:yoshiro,

You have already modified the controller and the view. It seems you only need to modify the model (adding your changes).

May I ask you what CRUD is?

Thanks
Create, Read, Update, Delete
http://en.wikipedia.org/wiki/Create,_re ... and_delete

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by pablofiumara » Tue Nov 19, 2013 8:23 am

Qphoria wrote:
pablofiumara wrote:yoshiro,

You have already modified the controller and the view. It seems you only need to modify the model (adding your changes).

May I ask you what CRUD is?

Thanks
Create, Read, Update, Delete
http://en.wikipedia.org/wiki/Create,_re ... and_delete
Thank you very much, Qphoria

User avatar
New member

Posts

Joined
Sun Nov 10, 2013 6:29 am


Post by rph » Tue Nov 19, 2013 8:52 am

That's a great start. I'd store the data in a new table like:

Code: Select all

CREATE TABLE `oc_country_description` (
 `country_id` int(11) NOT NULL DEFAULT '0',
 `language_id` int(11) NOT NULL DEFAULT '0',
 `name` varchar(128) NOT NULL,
 PRIMARY KEY (`country_id`,`language_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
Alternatively you could add a language_id column and indexes to the existing oc_country table though this could necessitate a lot of editing in other places.

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by yoshiro » Tue Nov 19, 2013 5:30 pm

--@Pablo

You are right basically this is where I like to start from cause sometimes it give you an idea about the model. But as I am new to Opencart I'm not 100% sure of things but now thanks to Ryan I have some tailwind to go on.

--@Ryan

Thanks Ryan this suits my needs perfectly, thanks to your positive feedback I can now go on to the next step.

If there is demand I'll update this thread as I go.

Newbie

Posts

Joined
Wed Nov 13, 2013 12:01 am

Post by butte » Wed Nov 20, 2013 9:42 am

yoshiro, in addition to uksitebuilder's vQmod Generator and its alternative versions, another tool you should see is for Shortcodes at quahar's http://forum.opencart.com/viewtopic.php?f=23&t=111369 with two releases, his 1.0 and most recently his 1.1 RC1 (at the moment the last post in the thread). As with the prior tool, I just want for you to see it, since it might come in handy.

Guru Member

Posts

Joined
Wed Mar 20, 2013 6:58 am

Post by yoshiro » Wed Nov 20, 2013 7:54 pm

@Buute
Thanks a lot that could be very useful !

I'm done with the backend part of this extension works pretty smooth now on to the frontend. Now the plan is to patch the country model at the frontend and change get_country() and get_countries() methods to try and get the translation for the current customer language and if it is null/not found fallback to the english name.

Newbie

Posts

Joined
Wed Nov 13, 2013 12:01 am

Post by yoshiro » Thu Nov 21, 2013 11:24 pm

I've finished my first extension!
Countries & Zones Translator

It's the first version i'd be happy if you download and check it out :)
please leave me feedback / suggestions / remarks etc.

I Thanks you all for your help.

Newbie

Posts

Joined
Wed Nov 13, 2013 12:01 am
Who is online

Users browsing this forum: No registered users and 8 guests