Creating and updating associative dynamic array for translations

I have an existing site that has been dual language. Now I started sending the ad text to a third party for translation in real time.

I have however decided that I should cache the results .

After looking at and modifying the code it seems easy to incorporate these cached results into the existing code. The code already has a translation array which I have added an additional array to . I can manually enter text into thsi array and have my custom translation appear rather the the live online translation . It is however my goal to write the live on-line translations into this array.

All I need to be able to do is to dynamically create the array. The format is:
[php]

<?php $lang = array( 'entry1' => "this would be the translation for entry1", 'entry2' => "this would be the translation for entry2", ); ?>

[/php]

Now i need to be able to figure out how to go about adding lines to this array and deleting old ones.

I figure it might be easiest to make this array a fixed number of entries say 1000 and I start with 1000 blank entries which can be filled in as needed . Then when all 1000 are full we would start deleting from the top (or bottom if that were the case) , the older entries, creating new lines at the bottom for new entries.

Maybe there is an easier way?

I have no clue how to do this but have the rest 99% ready to go

Help appreciated.

Well, I am not sure what you are asking, but, if you mean to remove the top item, it is indexed at “0”.
But, this requires some odd playing with the arrays to fix this issue… So, the easiest that I know of is just to
use the PHP functions made for this. TO remove the TOP level item in an array, use the PHP array_shift()
function. This function removes the top item in the array. Then, you can just append or concatenate the new
ones as needed. Just add new values as .= and it will add to the array.

Therefore, when you need to trim the array and also add to it, just use these two simple commands to do it.
The array_shift and add-to…

But, I wonder why you feel you need to send out 1000 items at a time? And, if you send out 1000 of them and
get them all back, why would you need to drop one and send back out the same 999 ones? Confused on what
you need help with on that part.

EDIT: I reread your post and realized you meant cache them locally on your site and not sending them all live
as you created them. I understand what you mean now. So, use the array_shift to solve it… You will have to
scan the array for dup’s, or almost dupes. One word different means a different value…

I do not need to send out 1000 items at a time.

This is a translation cache. That way after I query Yandex for a translation it gets held in the cache . lets say the last 1000 phrases. That way I do not have to ask yandex to translate the same phrases over and over again.

I appreciate your reply but I am unable to make too much sense out of what you are saying other than the PHP array_shift() function which I will look into immediately .

Thanks

Looks like i should probably start with 1000 bogus phrases?

Well, as I said, I reread your post and shifting the array will work. What it does is remove the top level entry.
So, everything gets moved up in the array. Then, you can add a new one at the bottom by just adding the new
entry to the array. Here is the link that explains it. I think it is simple enough to follow, but, ask any questions
and we can help…

http://php.net/manual/en/function.array-shift.php

Thanks the examples and explanations on php.net are never very good

for example they say

array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. How can I move everything down if I just removed the first element (leaving it blank)?

Maybe they should say moving everything up?

It means that it moves it DOWN in the indexing… Therefore, index 0 becomes whatever index 1 was. Or in an
example it would be like doing $index[0]=$index[1]; $index[1]=$index[2]; etc… So, index 0 goes away.
Adding to the index would add it at the end of the index…

So, up and down depends on the aspect of the viewer. Down in PHP.net meant down in indexes. No up in a list
that is in your mind. Lower indexes always mean down…

Hope that makes sense… Just test it out. Set up just three or four test items in an array and shift them and
use a print_r($array) to see what is lest in it…

Oh, also… If you want to keep it at 1000, you can use the count() function to count the items in the array.
Or, just do this using your database and keep a higher amount…

thanks but I do not get enough out of this to even try. They assume that my array is a single field I suppose and show no other syntax examples.

My array fields are like

‘leg’ => “pierna”,

Their examples are for single fields (numbered?)

Use a database for long term storage. Query the database for a phrase and language, return that value. The other option, create a localization json file and output based on the language. If you are already storing a translated value, there is no point in retranslating it.

Well, I mentioned moving to a database table for this as Astonechiper also said. But, for doing this in an array
the array_sift function will remove the first array entry. If it is “something”=>“else” this entire item is removed.
And, the second entry would become that first one. Did you test it?

This is NOT long term storage. These are classified ads that will expire.

As it is I am having trouble on the path that I am on now you suggest I go to a database which would require a massive code rewrite?

I am asking about the array as a text file as the code already supports. Please do not deviate from my needs.

All the more reason for database storage. Lets say the columns, id, lang1, lang2, loadedTimestamp

It loads, gets translated and stored till removal.

i am not rewriting the existing language calls in ALL of the code, so NO to the database and if you do not understand I say again NO DATABASE

please stop the database rant this question is NOT about a database

Well, just use the simple code functions I have told you about. They are simple and do what you want!
Did you try them.? NO! Or, you would already have the code working. Again, here is another sample to make
you understand how array_shift works. Just load this in one blank test page and see what you get out of it…

[php]

<?php $lang = array( "orange"=>"la-orange", "banana"=>"la-banana", "apple"=>"la-la-apple" ); echo "Original array:
"; print_r($lang); array_shift($lang); echo "

array-shifted one item:
"; print_r($lang); $lang["ernie"] = "alex"; echo "

added another item:
"; print_r($lang); ?>

[/php]
As easy as I can explain it to you… Good luck with it…

Been testing having zero luck with adding lines to the array.

Will check your examples better here

The problem I am having now is with adding the line to the array (file) and making it the right format array_push

Thanks

no clue actually I am lost. Array Push only appends the array in memory only as does the array shift so neither of these is working with the actual file rather the loaded array. I need to work with the file.

Well, you are confusing us again… Why did you change a simple array to using array_push. That’s a stack
function. And, you never talked about a file? Do you mean that you are attempting to keep the array inside
of a website file? If so, how are you loading the file? Is it a CSV file? A text file or are you saving the array
directly with a file write command? There are hundreds of ways to store data into files, but, in most cases, it
is a slow way to handle data. Especially if it changes often.

If you are talking about saving the array in a file, then you can convert to JASON or just dump it like this:
file_put_contents(“Myfile.xyz”, var_export($array, true));

Not tested, just loosely off the top of my head… Or you can serialize it and write it as a standard text file
and then unserialize it when it comes back in. Lots of ways… All depends on what you want it to do…

After some thought before bedtime…

Just use the array as I posted. Then, if needed to save it into a file, just use this version:

file_put_contents(“filename.xyz”, serialize($array));

That saves a text-savable version of the array. Then, to read it back do it like this:

$array = array();
$array = unserialize($file_get_contents(“filename.xyz”));

Simple… Just remember this is not a good way to handle data. It uses much more server resources than
a few lines of database code…

Some of you confused yourselves with your database mindset. I said from the beginning I am working with an array on a text file. The text file is where I need to focus my attention

loading the entire array in memory seems intensive, especially if later I make it much larger.

text operations could work in this case and some do not need to load the whole file in memory

Here is what I have come to so far but nothing I have tested has worked

I treat my en.inc.php and es.inc.php as a text files (spanish and english respectively.

I want to delete line 2 and append the file before the closing " )?> " tags in this case at line 999

A predefined file length of 1000 would be great as that will give me 998 lines of cached translations, and that is enough for now.

I can concatenate the text strings easily enough to enter the new line at line 2 (after line 999 is deleted ) and insert the new string at line 2

what I need are text string operations that do the following

  1. delete line 999 (not leaving a blank line rather deleting the entire line)
  2. insert new translation line at line 2

done

I do not believe that string operators care about the php at line 1 and line 1000

Sponsor our Newsletter | Privacy Policy | Terms of Service