Translate parts of a string individually using Twig and the i18n extension? - internationalization

I have a string that looks like
This is a list of all items with number 123456 in United States.
and I want to translate it to Swedish as
Detta är en lista över alla artiklar med nummer 123456 i USA.
The problem is that the number 123456 and the country name United States are generated dynamically, but the string is delivered in its final form to the Twig template (it's used for <meta name="description" ... />.
I already have a .po file with country names that I'm translating, including
English Swedish
United States USA
The generated meta description string can have any combination of numbers and country names, so I can't hardcode any translations for the entire string. Is there any way I can re-use my existing translations for the country names and have them translated inside the meta description string, and have the number just stay as it is? Or do I need to somehow split the string up first, and then translate each part individually?

I am not a PHP programmer, so I use pseude code:
num_items = 42;
country = "United States";
string = sprintf(gettext("... items with number %d in %s"),
num_items, gettext(country));
Is that what you are looking for?

Related

Laravel 5.1 translate an array pass to blade

I have this in my controller
public function editProfile(Request $request){
$question1 = Question::where('group',1)->lists('question', 'id');
$question2 = Question::where('group',2)->lists('question', 'id');
return view('user', compact(''question1', 'question2'));
}
$question = {\"1\":\"What is the first name of your best friend in high school?\",\"2\":\"What was the name of your first pet?\",\"3\":\"What was the first thing you learned to cook?\",\"4\":\"What was the first film you saw in the theater?\"}
$question2 = {\"5\":\"Where did you go the first time you flew on a plane?\",\"6\":\"What is the last name of your favorite elementary school teacher?\",\"7\":\"In What city or town does your nearest sibling live?\"}
I would like to translate the value in both question 1 and question 2 and pass it to user blade without changing the key, Any suggestions?
As specified at the localization doc, you need to populate a lang file with translation strings. So, let's say you want to show both Spanish and English strings. In this case you'd need to create two files: /resources/lang/en/messages.php and /resources/lang/es/messages.php. The content of one of those files would be somewhat like this:
<?php
// resources/lang/es/messages.php
return [
'welcome' => 'Bienvenido'
];
This way, you could access the strings in there with the following method: __('messages.welcome'), which would return the string for the language set on your config/app.php -- the default entry is en, by the way, but you can set it to whatever you want. The value in there will define which language will be chosen when selecting strings.
Another method to create translation strings is using the string itself as key, storing them in a JSON file instead of PHP. For example, the following translation string:
{
"I love programming.": "Me encanta programar."
}
would be accessible through this: __('I love programming.').
Having said that, you may solve your problem through the two methods presented above. You could store in your DB keywords for the questions instead of the whole text, and create translation for as many languages as you want. Also, you could keep the questions in your database and create translation strings for those questions. Finally, you'd need to iterate over the fetched entries and translate each one of them, or use some Collection helper to do the hard work for you, like transform or map.

Ajax call to wiktionary.org to retrieve information on one specific language

To get information about a word in Wiktionary, I can make an Ajax call to the following URL at wiktionary.org: https://en.wiktionary.org/w/api.php?action=parse&format=json&prop=text|revid&callback=?&page=слово, where слово is the word of interest.
This will return a JSON object with the format...
{ "parse":
{ "title": "\u0442\u044b"
, "pageid":96216
, "revid":38162039
, "text":{
"*": <HTML string>
}
}
}
... where <HTML string> contains information on the given word, in all the languages that Wiktionary associates with the word. In the case of the word слово, this means
Bulgarian
Macedonian
Old Church Slavonic
Russian
Serbo-Croatian
Ukrainian
How can I change the URL for the Ajax call so that it only returns data for a single language (for example: Russian)?
It's not possible as in terms of Wiktionary each word is a page with wiki-formatted text. It's not structured as a JSON or any other machine readable data structure format. Though, they have a convention which every page is supposed to follow. According to it, each page should have level 2 headings with word's language. So, you can parse the wikitext to extract only the data for the language you want by looking for such a heading and taking only the data below it.

Get Month Name of java.time.chrono.HijrahDate instance

HijrahDate hd=HijrahChronology.INSTANCE.date(LocalDate.of(2014,11, 25));
If we have HijrahDate Instance , it is expected to have a method in UmalquraCalendar API that shows the name of month :
i inspect properties of this instance using groovy API :
['era':AH,
'class':class java.time.chrono.HijrahDate,
'prolepticMonth':17233,
'eraValue':1,
'dayOfWeek':2,
'leapYear':false,
'chronology':Hijrah-umalqura,
'dayOfYear':32]
However we don't find the month name which must be one of the following list items :
Muḥarram (محرم meaning "forbidden"), so called because battle was
forbidden (haram) during this month. Muharram includes the Day of
Ashura.
Ṣafar (صفر meaning "void"), supposedly named thus because
pagan Arab houses were empty this time of year while their occupants
gathered food.
Rabīʿ I (Rabīʿ al-Awwal, ربيع الأوّل) meaning "the
first spring".
Rabīʿ II (Rabīʿ ath-Thānī ربيع الثاني or Rabīʿ al-Ākhir
ربيع الآخ
.....................
............ so on SEE
Thus , since there is no attribute save month's name , it is expwcted to have a method retrieve this info ?
What's this method?
The date does not contain information about the names of the months or days. To get that you need a formatter:
System.out.println(DateTimeFormatter.ofPattern("MMMM").format(hd));
prints Safar.
Since the main language of UmalQura is the arabic langugage, Developers & programmers who uses UmalQuraCalender want to display the month in arabic. Thus , we base on #assylias answer we can add the Locale object to print صفر instead of Safar
System.out.println(DateTimeFormatter.ofPattern("MMMM").format(hd,new Locale("ar")));
public String getIslamicDate(){
return DateTimeFormatter.ofPattern("MMMM",new Locale("ar")).format(HijrahDate.now());
}
I think this should work just fine and return the month in arabic language

get only city names in the Google Directions API

I am using the Google direction API to get itineraries based on points that can be cities only (not addresses, countries, or any POI).
I would like to parse the city name out of the response, but the API return a somewhat cumbersome string that includes postcodes, states, countries, etc. Nevertheless, it appears that this format is not always the same, which makes it somewhat dangerous to try to get a sub-string out it. Example:
link 1 This call returns Australian locations names with postcodes, states, and country.
"start_address": "Albury NSW 2640, Australia",
"end_address": "Bendigo VIC 3550, Australia",
link 2 this call returns French locations that have no region nor postcode.
start_address": "Reims, France",
end_address": "Metz, France",
Any thoughts?
The only known solution relies on parsing the string :
$end_address = $GoogleResponseArray['routes'][0]['legs'][$x]['end_address'];
$end_address = explode(",", $end_address)[0];
$end_address = explode(" ", $end_address)[0];

Perform a search with LINQ and ignoring characters like î or ô or ê

In my DB I have some texts like this one: 'AVENUE GEORGES LEMAîTRE'. As you can see there is a ^ on the 'i' character.
Is it possible to perform a search with LINQ and ignoring this special character.
So I would like to search for 'AVENUE GEORGES LEMAITRE' and find 'AVENUE GEORGES LEMAîTRE'.
var address = AddressRepository.Find(m => m.Street == "AVENUE GEORGES LEMAITRE").ToList();
Possible? How?
Thanks.
try:
List<String> address = (from ad in addressRepository where string.Compare("AVENUE GEORGES LEMAITRE", ad.Street.ToUpperInvariant(),CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) == 0 select ad.Street).ToList();
Otherwise
This blog entry might help:
It details how to remove Diacritics from strings.
go to the bottom for the code sample.
Once the Characters are replaced the strings can be compared.
Hope this helps.
http://www.siao2.com/2007/05/14/2629747.aspx
You should modify your database model to include a "canonical" version of Street along with the "actual" version that you display to or accept from the user. This canonical version could for example be all uppercase, with all diacritics removed, and normalized to a specific Unicode normalization form.
Assuming that you have a method Canonicalize that does this (this might be a good place to start), you would invoke it when saving an address:
var address = /* incoming data */
address.CanonicalStreet = Canonicalize(address.Street);
/* save the model */
And you would also use it when retrieving data:
var street = "AVENUE GEORGES LEMAITRE";
street = Canonicalize(street);
var address = AddressRepository.Find(m => m.CanonicalStreet == street).ToList();

Resources