Is it possible to get translation from language file that contains (.)dot?
For example, The language directory looks like
/lang
/en
pagination.php
phone.history.php
/es
pagination.php
phone.history.php
phone.history.php file contains dot and want to get translation for the key 'name' from phone.history.php file.
Lang::get('phone.history.name')
It doesn't work.
Actually the Laravel key used for translations can only contain one dot . in order to work and the function of that dot is to separate the translation file path (relative to the language directory) from the translation array key. That's because the Translator class extends the Illuminate\Support\NamespacedItemResolver class, which parses the key using the following logic:
First it determines if there is a namespace present (denoted by ::) and extracts it.
It then goes on to split the key into a group (which is the relative filepath) and an item (which is the translation array key).
So in your case since there is no namespace, when you use this:
Lang::get('phone.history.name')
The result is the following:
The filename will be phone
The translation array key will be history (instead of name)
And since there's no phone.php in your language directory, it won't work.
So you have two options here:
Use another character in your filename so there's only one dot in the translator parameter, something like /lang/en/phone_history.php and then use Lang::get('phone_history.name').
Use a subdirectory if you want to group some translation files, something like /lang/en/phone/history.php and then use Lang::get('phone/history.name') (unfortunately, as I said above you can only use one dot as a separator, so you can't use the multiple level dot notation as you would for a configuration file Lang::get('phone.history.name')).
There is actually a 3rd option which is to use dotted key names. So inside /lang/en/phone.php you can set 'history.name' => 'John Doe' and then retrieve that using trans('phone.history.name').
Related
What I need Is to go thru a directory that contains subdirectories. In each second level directory has a third level directory that contains a file with same file name for every second level directory.
In each of those files made in PHP there Is a variable that contains like: appversion = 'YYYYMMDD'. I need to modify this date for each appearance To eg appversion = '20190301'.
How to do this In Shell or with PHP?
Edit: sorry for the malformed question, this is what defines better what I need to do really
I am afraid the question was a little malformed.
What I need to do is:
1. To find in all subdirectories a file called "productver.php".
2. In each "productver.php" file find a variable called 'appversion'.
3. Replace the value with a given value that i provide.
Example: I provide a date that is "20190324" and I want to change this to the value of all 'appversion' variables. E.g. 'appversion = 20180121' ==> 'appversion = 20190324'. For this reason, I am afraid simple replace is not enough as I only need to replace the part which is on the right side of the "=" character. And I cannot seek for a specific date, because the dates of this variable change. All I know is that I need to replace those dates, on the right side of the "=" sign of variable 'appversion'.
Let's say I have a folder (folder_1) with the following structure:
/folder_1
/dir_1
- file_1_1.txt
- file_1_2.txt
/dir_2
- file_2_1.txt
/dir_2_1
- file_2_1_1.txt
- file_1.txt
Now, let's say I have another folder (folder_2) with the following structure:
/folder_2
/dir_1
- file_1_1.txt
- default.txt
/dir_2
- file_2_1.txt
- default.txt
- default.txt
I need to map every file in folder_1 to a file in folder_2 such that:
/folder_1/dir_1/file_1_1.txt maps to /folder_2/dir_1/file_1_1.txt.
/folder_1/dir_1/file_1_1.txt maps to /folder_2/dir_1/default.txt
/folder_1/dir_2/file_2_1.txt maps to /folder_2/dir_2/file_2_1.txt
/folder_1/dir_2/dir_2_1/file_2_1_1.txt maps to /folder_2/dir_2/default.txt
/folder_1/file_1.txt maps to /folder_2/default.txt
I am not the best communicator, so hopefully, the above pattern makes sense to you guys. The question is language agnostic really, but an answer in PHP and/or Javascript would be really great.
So far, I was able to accomplish this in PHP using FileIterator, RecursiveDirectoryIterator, and a bunch of custom classes that extract and then map the path to the files one by one.
This makes me wonder if I am missing an easier way to do this simple mapping. Maybe using regex named groups or something?
**Edit: **
Is it possible that for each file (file path) in folder_1, we use a regex pattern to find (reduce) the best match out of a map of all file paths in folder_2?
Further edit:
This is for mapping data files in folder_1 to template files in folder_2. If for a file in folder_1, an exact matching file path (including filename) in folder_2 is not found, we look for default.txt. If default.txt is not found, then we move up a directory and use that parent directory's default.txt. This way, we keep moving up directory levels till we find the first default.txt.
First, use your recursive directory scanner to scan all of the folder_2 directory tree. Build a hash table that contains the file names, without the folder_2 prefix. So your hash table would contain:
/dir_1
/dir_1/file_1_1.txt
/dir_1/default.txt
/dir_2/file_2_1.txt
/dir_2/default.txt
/default.txt
Now, start scanning folder_1. When you get a file, strip folder_1 from the front, and look for the resulting string in the hash table. If it's there, then you have a match.
If the file is not there, replace the last segment with "default.txt", and try again. So, when you begin scanning folder_1, you get:
/folder_1/dir_1/file_1_1.txt
You look up dir_1/file_1_1.txt in the hash table and find it. You have a match.
Next, you get /folder_1/dir_1/file_1_2.txt. You look up /dir_1/file_1_2.txt in the hash table and don't find it. So you replace file_1_2.txt with default.txt, giving you /dir_1/default.txt. You look that up in the hash table, find it, and you have a match.
Now, if /dir_1/default.txt did not exist, then you would again adjust the file name to remove the last directory. That is, you'd remove /dir_1, and you'd look up /default.txt in the hash table.
In pseudo code it looks like this:
for each file in folder_1
name = strip `/folder_1` from the name
if name in hash table then
match found
continue (next file)
end if
replace file name (everything after the last '/') with "default.txt"
do
if name in hash table then
match found
continue (next file)
end if
remove the last slash, and everything between it and the previous slash.
(so "/dir_1/default.txt" becomes "/default.txt")
while name.length > 0
// if you get here, no match was found
end for
I 'm from peru present .
what happens is that eh followed a tutorial on gettext and CodeIgniter and I can not get it to work , I only translates the text into Spanish peru .
the only thing that changed was the helper eh , that after writing the function , execute it as follows . set_translation_language ( en_EU ) , or where I have to run this function . ?
From already thank you very much.
I suggest you to take a look at this: http://www.codeigniter.com/userguide2/libraries/language.html
CodeIgniter already support multi-language (location).
If your site will support only one language (Spanish), there is no need to use a location service.
But if you plan to support English and Spanish for example, what you need to do is:
Create a folder with the language name (ie: English)
Add a file with "_lang.php" (no quotes) at the end (ie: text_lang.php)
Create an array under the value $lang, each object inside will contain the
key, for example: $lang['title'] = "This is the title."; (English)
and then on the Spanish folder, the same but translated:
$lang['title'] = "Este es el tÃtulo.";
Load the language file: $this->lang->load('text_lang', 'spanish'); that's valid in our example.
Last, but not least, load the language line you want to translate, for example, title $this->lang->line('title');
Not sure if the title for this is correct but here's my problem:
I have a table that hold image's url like so:
img/folder/imagename.jpg
Now I've created a thumbnail for each image in each folder, so if I want to display them I do a loop in my table and return all the url's but I need to add the word "thumbs" after "folder/" and before the "imagename.jpg"...Now because obviously the "folder" and "imagename" names differ in length then I can't do a count..the only thing I can figure is to look up that last "/" character and insert there and add on the "imagename.jpg" after it so end result would look like:
img/folder/thumbs/imagename.jpg
Just replace the path with a "deeper" path:
$path = str_replace('img/folder/', 'img/folder/thumbs/', $path);
There are lots of other ways to do this, but IMHO this one is perfectly clear on what it does. Theoretically it won't work universally (if your path contains multiple occurreces of img/folder/ for some reason), but let's just not go there.
you can also
$pathparts = explode("/",$path);
and then use $pathparts array to construct your path again. You would use it in cases you want to have more control over manipulating paths, but in heavy loads it's not very efficient.
echo $pathparts[0]."/".$pathparts[1]."/thumbs/".$pathparts[2];
//adition
And why not to update your database and script only save imagename.jpg
and then in the beginning of calling script define
define("IMGPATH", "img/folder/");
define("THUMBPATH", "img/folder/thumbs/");
and then call it
<?= IMGPATH."imagename.jpg" ?>
<?= THUMBPATH."imagename.jpg" ?>
I'm implementing a php interface to process a .php file containing a bunch of define sentences defining constants with text values to translate by somebody.
The input is parsed sentence by sentence and shown in the interface to the translator who will input the translation in a html textarea and send it to the server
By the end of the process an output file would be generated, identical to the input .php file, only with the define values translated.
E.g. processing an input file 'eng.php' like this:
<?php
define ("SENTENCE_1", "A sentence to translate");
?>
would give the translated output file 'spa.php' like this:
<?php
define ("SENTENCE_1", "The same sentence translated to spanish");
?>
For this I would like to know:
What is the best way to parse the input file to get the constant names and values in an array? (Something like $var[0]['name'] would be "SENTENCE_1" and $var[0]['value'] would be "A sentence to translate")
Would it be possible to get the translation from google translator shown in the input textarea as a suggestion to edit for the person who is translating it? How? (I read google translator api v1 is no longer available and v2 is only available as a paid service. Are there any free alternatives?)
http://php.net/manual/es/function.get-defined-constants.php
What about that?
get_defined_constants doesn't give you exactly the structure you asked for, but it should be sufficient.
define('MY_CONSTANT', 'something');
define('MY_CONSTANT_2', 'another');
$constants = get_defined_constants(true);
$constants = $constants['user'];
print_r($constants);
/**
* array(
* 'MY_CONSTANT' => 'something',
* 'MY_CONSTANT_2' => 'another'
* )
*/
Note that this will be all constants defined in the current scope, which in PHP is gonna be anything defined this request.
Use get_defined_constants() to get the list of all the defined constants.
To get userdefined constant specially
$allconstants = get_defined_constants(true);
print_r($allconstants['user']);
In case anybody needs to read constants' names and values defined in a given .php file into an array of variables without actually defining those constants (E.g. if some different constant with the same name was previously defined, thus giving an error when processing the file with include or require), here is how I did it (Warning: I haven't had any trouble yet, but it's not thoroughly tested, so it can be buggy).
if (file_exists($filename)){
$outf=fopen($filename,'r');
while (($line=fgets($outf))!==false){
if (strpos($line, 'define')!==false){
$parts=explode("\"",implode("\"",explode("'",implode("\\q",explode("\\\"",implode("\\s",explode("\\'",$line)))))));
$name=implode("\\'",explode("\\s",implode("\\\"",explode("\\q",$parts[1]))));
$value=implode("\\'",explode("\\s",implode("\\\"",explode("\\q",$parts[3]))));
$outconstants[$name]=$value;
}
}
}
You can see I assume there's no more than 1 define sentence per line, and that the names and values of the constants are specified as string values using PHP notation (between single (') or double (") quotes.)
Also, escaped quotes (\" or \') are temporarily escaped as \q (\") or \s (\') instead, to properly match the non-escaped ones, and then escaped back as usual once what's in between the non escaped ones is assigned to $name and $value.
The google api problem was solved using microsoft translation api instead (free up to 2.000.000 chars/month): http://msdn.microsoft.com/en-us/library/ff512421.aspx#phpexample