0

Hope that you are doing file

So here is my question i have a xyz.string file that is used for translation.Please find the a small part of file below.

/* name for an item that is duplicated in the UI, based on the original name */
"%@ (Copy)" = "%@ (kopi)";

/* display name for a book page template that is the first page of that section */
"%@ (First)" = "%@ (Første)";

/* display name for a book page template that represents a hardcover cover. the second argument is the cover type. */
"%@ (Hardcover, %@)" = "%1$@ (Hard innbinding, %2$@)";

/* display name for a book page template that represents a softcover cover. the second argument is the cover type. */
"%@ (Softcover, %@)" = "%1$@ (myk innbinding, %2$@)";

i want to convert the translation i.e in a php array like

array(
array{
"%@ (First)"=>"%@ (Første)"
},
array
{
"@ (Hardcover, %@)"=>"%1$@ (Hard innbinding, %2$@)"
}
)

and so on.The format that is specified is not mandatory but it should be something that i can workaround with.

Following is the specification of the file format

  • key-value pairs are delimited with the equal character (=), and terminated by a semicolon (;).

  • keys and values are surrounded by double quotes (“)

  • place-holders look can be: %.2f, %d, %1$s (regular expression for placeholders: /%[\d|.]\$\d*[dsf]{1}\b+/)

  • comments start at the beginning of the line and span the whole line or multiple lines

  • single-line comments start with double slashes (//) multi-line comments are enclosed in /* */

  • a comment is assigned to the next key-value pair unless there are any blank lines in between

I know this can be achieved through PREG_MATCH_ALL but i am not able to create a nice regular expression

below is my code

$str=file_get_contents($_FILES['string']['tmp_name']);
preg_match_all("|(\".+\"\;)|s",preg_quote($str),$match);
echo "<pre/>";print_r($match);die;

the exact string i am getting after file_get_content is following

/* name for an item that is duplicated in the UI, based on the original name */ "%@ (Copy)" = "%@ (kopi)"; /* display name for a book page template that is the first page of that section */ "%@ (First)" = "%@ (Første)"; /* display name for a book page template that represents a hardcover cover. the second argument is the cover type. */ "%@ (Hardcover, %@)" = "%1$@ (Hard innbinding, %2$@)";

If anyone can help me in this it will be much appreciated

Thanks Ryan

2 Answers 2

1

The Lord, Regex:

"([^"]+)"\s*=\s*"([^"]+)";

Explanations:

"([^"]+)"     # Every thing between double quotes
\s*=\s*       # Equal sign preceded or followed by any number of spaces
"([^"]+)";    # Again every thing between double quotes that ends to a ;

PHP code:

$text = file_get_contents($_FILES['string']['tmp_name']);
preg_match_all('#"([^"]+)"\s*=\s*"([^"]+)";#', $text, $match);
$translate = array_combine($match[1], $match[2]);
print_r($translate);

The output for your sample text will be:

Array
(
    [%@ (Copy)] => %@ (kopi)
    [%@ (First)] => %@ (Første)
    [%@ (Hardcover, %@)] => %1$@ (Hard innbinding, %2$@)
    [%@ (Softcover, %@)] => %1$@ (myk innbinding, %2$@)
)
Sign up to request clarification or add additional context in comments.

3 Comments

i thought it would work but its not working i am not getting anything in the match array please find the exact string that i am getting after file_get_content in my nxt comment
/* name for an item that is duplicated in the UI, based on the original name / "%@ (Copy)" = "%@ (kopi)"; / display name for a book page template that is the first page of that section / "%@ (First)" = "%@ (Første)"; / display name for a book page template that represents a hardcover cover. the second argument is the cover type. */ "%@ (Hardcover, %@)" = "%1$@ (Hard innbinding, %2$@)";
yes its working with the string but not with the file content "STRANGE" i think its a file encoding problem following the link to the .strings file.if you can give it a look it would be great. dropbox.com/s/exj32uauknjut2x/Localizable.strings
0

You could just read the wohle string and then use: explode

$array = explode(';', $string);

3 Comments

explode won't work as the comments in the top can have ; and comments will also come in the array
Oh, ok! How about: explode('";' ."\n", $string)?
nops the str that i am getting doesn't have a new line moreover it is not reliable as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.