how do I generate po file from php array? - localization

I've a php language file, that lists values in an array.
I want to transform this into .po file.
php file looks like this:
<?php
$LANG_ = array(
// advanced search buttons
"_search1" => "Start Search",
"_search2" => "Hide Search Box",
"_search3" => "Search",
// page numbering
"_pn1" => "Page %CURRENT_PAGE% of %TOTAL_PAGES%",
"_pn2" => "<< First",
"_pn3" => "Last >>",
// _tpl_article-add.php
"_tpl_article-add1" => "Article Submission Form",
"_tpl_article-add2" => "Submit Article",
"_tpl_article-add3" => "Article Submitted Successfully",
// errors
"_err14" => "Delete",
"_err144" => "Display Image",
"_err15" => "Edit",
"_err16" => "View",
);
?>
This is just an example, file itself is huge, over 3000 lines. It would kill me to insert every single one of these into a po catalog manually. Is there something that can automate this for me?
I'm using poedit.
Thanks, I'm new to this, so any insight will be useful...

Try php2po which will convert a PHP array into PO files and will also make sure that all of your escaping is correct. The PO file can then be edited in a tool like Poedit or Virtaal.

The first entry is the file header, look into an existing gettext file (-po) what is needed. The escaping I did with addslashes; maybe you need to do more.
$fh = fopen("en.po", 'w');
fwrite($fh, "#\n");
fwrite($fh, "msgid \"\"\n");
fwrite($fh, "msgstr \"\"\n");
foreach ($LANG_ as $key => $value) {
$key = addslashes($key);
$value = addslashes($value);
fwrite($fh, "\n");
fwrite($fh, "msgid \"$key\"\n");
fwrite($fh, "msgstr \"$value\"\n");
}
fclose($fh);

Related

Ruby - link_to - How to add data directly from DB

First of all, I am very new to ruby and I am trying to maintain an application already running in production.
I have been so far able to "interpret" the code well, but there is one thing I am stuck at.
I have a haml.html file where I am trying to display links from DB.
Imagine a DB structure like below
link_name - Home
URL - /home.html
class - clear
id - homeId
I display a link on the page as below
< a href="/home.html" class="clear" id="home" > Home </a>
To do this I use 'link_to' where I am adding code as follows
-link_to model.link_name , model.url, {:class => model.class ...... }
Now I have a new requirement where we have a free text in DB, something like -
data-help="home-help" data-redirect="home-redirect" which needs to come into the options.
So code in haml needs to directly display content versus assign it to a variable to display.
In other words I am able to do
attr= '"data-help="home-help" data-redirect="home-redirect"' inside the <a>, but not able to do
data-help="home-help" data-redirect="home-redirect" in <a> tag.
Any help would be greatly appreciated!
link_to accepts a hash :data => { :foo => "bar" } of key/val pairs that it will build into data- attributes on the anchor tag. The above will create an attr as follows data-foo="bar"
So you could write a method on the model to grab self.data_fields (or whatever it's called) and split it into attr pairs and then create a hash from that. Then you can just pass the hash directly to the :data param in link_to by :data => model.custom_data_fields_hash
This somewhat verbose method splits things out and returns a hash that'd contain: {"help"=>"home-help", "redirect"=>"home-redirect"}
def custom_data_fields_hash
# this would be replaced by self.your_models_attr
data_fields = 'data-help="home-help" data-redirect="home-redirect"'
# split the full string by spaces into attr pairs
field_pairs = data_fields.split " "
results = {}
field_pairs.each do |field_pair|
# split the attr and value by the =
data_attr, data_value = field_pair.split "="
# remove the 'data-' substring because the link_to will add that in automatically for :data fields
data_attr.gsub! "data-", ""
# Strip the quotes, the helper will add those
data_value.gsub! '"', ""
# add the processed pair to the results
results[data_attr] = data_value
end
results
end
Running this in a Rails console gives:
2.1.2 :065 > helper.link_to "Some Link", "http://foo.com/", :data => custom_data_fields_hash
=> "<a data-help=\"home-help\" data-redirect=\"home-redirect\" href=\"http://foo.com/\">Some Link</a>"
Alternatively you could make it a helper and just pass in the model.data_attr instead
link_to "Some Link", "http://foo.com/", :data => custom_data_fields_hash(model.data_fields_attr)
Not sure you can directly embed an attribute string. You could try to decode the string in order to pass it to link_to:
- link_to model.link_name, model.url,
{
:class => model.class
}.merge(Hash[
str.scan(/([\w-]+)="([^"]*)"/)
])
)

using Foreach to make a table

Im new to PHP and I made this to display to my website what has been uploaded in the fdpp portal
<?php $resp = file_get_contents("http://fdpp.blgs.gov.ph/api/documents?source=datatable&sSearch=kalinga");
$clean = json_decode($resp);
print_r($clean); ?>
this is the result :
stdClass Object
(
[iTotalRecords] => 130035
[iTotalDisplayRecords] => 879
[sEcho] => 0
[aaData] => Array
(
[0] => stdClass Object
(
[lgu] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129293'>CAR<br/>Kalinga<br />Balbalan</a>
[document] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129293'>Local Disaster Risk Reduction and Management Fund Utilization (LDRRMF)</a>
[period] => Quarter 1 2014
[status] => Required • SUBMITTED
[desc] => The atng.
)
[1] => stdClass Object
(
[lgu] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129188'>CAR<br/>Kalinga<br />Balbalan</a>
[document] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129188'>Manpower Complement</a>
[period] => Quarter 1 2014
[status] => Required • SUBMITTED
[desc] => The file ag.
)
what shall I add to my code to put this in a table with column name lgu, document, period? I tried reading foreach manual but I cant figure it out can someone help me?
I have to agree with PeeHaa (+1) with not enough information given, but I'm going to attempt to help you out.
The following assumptions were made:
You have a local MySQL DB named "testdb"
You have the PDO extension installed
In "testdb", you have a table named "test_table" with 4
columns (id, lgu, document, period)
First, you need to set the second parameter in json_decode to true to return an associative array rather than an object (PHP: json_decode).
So, based upon the limited information, here is a working version:
<?php
$resp = file_get_contents("http://fdpp.blgs.gov.ph/api/documents?source=datatable&sSearch=kalinga");
$clean = json_decode($resp,true);
// Open connection to your MySQL DB
$db = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
// Parse the now-associative array and insert into table
foreach($clean['aaData'] as $doc){
$stmt = $db->prepare("INSERT INTO test_table(lgu,document,period) VALUES(:lgu,:document,:period)");
$stmt->execute(array(':lgu' => $doc['lgu'], ':document' => $doc['document'], ':period' => $doc['period']));
}
?>
You should really provide some more information for a more exact answer.
But, this should set you in the right direction.

twitter-bootstrap dropdown-toggle opened on display and doesn't toggle

I have inserted a test toggle button ( pasted from demo)
#menu1.dropdown
%a.dropdown-toggle{:'data-toggle' => "dropdown", :href => "#menu1"}
Options
%b.caret
%ul.dropdown-menu
%li
= link_to "Action", "#"
%li
= link_to "Another action", "#"
%li
= link_to "Something else here", "#"
%li.divider
%li
= link_to "Separated link", "#"
The button is displayed but all options are visible and no toggle happen at all
The button-toggle.js plugin is present ( checked in assets ) and I already added into my application js file
$(document).ready ->
$('.dropdown-toggle').dropdown()
It's very strange , because at the top of my page , in the navigation bar , I already have a dropdown button to select the site language ... and this one is working fine ... ( displaying both the languages and flags )
.btn-group{:style => "margin-top: 4px; float:right;"}
%a.btn{:'data-toggle' => "dropdown", :href => "#", :id => "babLocaleSelect"}
%span.babFlag{:class => "babFlag-#{I18n.locale}"}= I18n.locale.to_s
= I18n.t(I18n.locale)
%a.btn.dropdown-toggle{:'data-toggle' => "dropdown", :href => "#"}
%span.caret
%ul.dropdown-menu.bablevel-content
- other_backoffice_languages.each do |language|
%li.babLocale
%a{:href => backoffice_language_path(:locale => language), :id => "#{language}_language_link" }
%span.babFlag{:class => "babFlag-#{language}"}= I18n.t(language)
= I18n.t(language.to_sym)
I tried also to debug , inserting
$(".dropdown-menu").click (event) ->
alert 'dd clicked'
this is triggered by BOTH menus so it's installed but the second is open and never toggle
Nothing related to Twitter-bootstrap ..
I tried to move the toggle-button in other places of my template , first in my top nav bar as the first one is running, and this test runs well...
I place it in another part of the template , same results : it runs ...
so I looked into the class used for this button : .pagination , which is the class also used by Kaminari paginator ... I get rid of this class ... and it runs !!

Haml Hyperlink inside a label tag

Inside my .haml file, I have
= employee.label :tos_confirmation, 'I agree to the Terms of Service.', :class => 'wide'
How do i make Terms of Service a hyperlink?
= link_to employee.label(:tos_confirmation,
'I agree to the Terms of Service.',
:class => 'wide'),
'http://example.com/tos'
The answer I was looking for is like this:
%label.wide{:for => "tos_confirmation"}
I agree to the
%a{:href => "/tos"} Terms of Service
and
= succeed "." do
%a{:href => "/privacy_policy"} Privacy Policy
The only part I'm missing is the "employee" part, which is not too important.

Problems passing special chars with observe_field

I am working on a rails project. Using the tag observe_field, I am taking text typed into a text area, processing it in a control, and displaying the result in a div (very similar to the preview in stack overflow). Everything works fine until I type certain special chars.
? => causes the variable not to be found in the params object
(pound) => causes an invalid authenticity error
% => stops the div from being updated
& => every thing after the & is no longer passed into the variable on the server.
Is there a way to solve this?
--- code sample ---
this is the view. ( 'postbody' is a text area)
<%= observe_field 'postbody',
:update => 'preview',
:url => {:controller => 'blog', :action => 'textile_to_html'},
:frequency => 0.5,
:with => 'postbody' -%>
this is the controller that is called
def textile_to_html
text = params['postbody']
if text == nil then
#textile_to_html = '<br/>never set'
else
r = RedCloth.new text
#textile_to_html = r.to_html
end
render :layout => false
end
and this is the javascript that is created:
new Form.Element.Observer('postbody', 0.5, function(element, value) {new Ajax.Updater('preview', '/blog/textile_to_html', {asynchronous:true, evalScripts:true, parameters:'postbody=' + value + '&authenticity_token=' + encodeURIComponent('22f7ee12eac9efd418caa0fe76ae9e862025ef97')})})
This is an escaping issue (as stated by others).
You'll want to change your observe_field :with statement to something like:
:with => "'postbody=' + encodeURIComponent(value)"
Then in your controller:
def textile_to_html
text = URI.unescape(params['postbody'])
...
Can you provide a code sample?
More likely than not you'll just need to escape your HTML entities using encodeuri or something like that.
What does the generated Javascript look like?
Sounds (at first glance) like it's not being escaped.

Resources