Jump to content
Friedrich

Adding image field to language

Recommended Posts

Hello,

I noticed today some strange behaviour in connection with the multi-language plugin.

I wanted to create a multi-language page and to display the languages in the frontend. To make it more fancy I wanted to add a flag icon to every language. So I used an image field I already had (image_small) and added it to the language-template. So far so good. Then I uploaded two icons and tried to fetch them in the frontend.

I have the languages default (English/Englisch) and de (German/Deutsch) and the following code:

<?php
echo 'active|'.$user->language->image_small->url;
foreach($languages as $language) {
    echo $language->title.'|'.$language->image_small->url;
}
?>

Result on an english page:

active |/site/assets/files/1021/united-states.png
English |/site/assets/files/1021/united-states.png
Deutsch|/site/assets/files/1081/
 
Result on a german page:
active|/site/assets/files/1081/
English|/site/assets/files/1021/
Deutsch|/site/assets/files/1081/
 
Somehow he can't get the German flag image at all and only the English page the English flag.
 
But if I'm fetching the images from the $languages variable, it works. So I use this temporary as a workaround:
 
<?php
echo 'active|'.$languages->get($user->language->name)->image_small->url;
foreach($languages as $language) {
    echo $language->title.'|'.$languages->get($language->name)->image_small->url;
}
?>
 

Result English:

active|/site/assets/files/1021/united-states.png
English|/site/assets/files/1021/united-states.png
Deutsch|/site/assets/files/1081/german.png
 
Result German:
active|/site/assets/files/1081/german.png
English|/site/assets/files/1021/united-states.png
Deutsch|/site/assets/files/1081/german.png
 
Everything is fine with this workaround. But why isn't working with the first code? Thanks for any help.
 
Environment:
PHP 5
ProcessWire 2.7.2

Share this post


Link to post
Share on other sites
Environment:
PHP 5
ProcessWire 2.7.2

Can you please be more specific on the version of PHP that you are using (i.e. 5.3.8, 5.4, 5.5 or 5.6)?

Share this post


Link to post
Share on other sites

I've stumbled over the same behavior a few days ago running PHP 7 and PW version 3.0.15, so it's not limited to either of those.

Share this post


Link to post
Share on other sites

I've stumbled over the same behavior a few days ago running PHP 7 and PW version 3.0.15, so it's not limited to either of those.

That's good to know and will help in getting this problem resolved.  The question was asked because the OP stated PHP 5, which would be less than what's required to run ProcessWire.

Share this post


Link to post
Share on other sites

Sorry, forgot to mention it. It is PHP 5.5.9.

Oh, you had the same error in version 3. Then it seems to be not fixed yet (if this is actually a bug).

Share this post


Link to post
Share on other sites

Hello. I just decided today to get rid of the language names and use flags for a project of mine and got the same issue mentioned by @Friedrich and @BitPoet after setting my languages to Bulgarian (default), English and Russian. For both  - the Russian and English language the icon shows promptly, however only for the Bulgarian one (most likely for the default one) the url does not point to the file but to the folder with the language ID (in my case: site/assets/files/1010

To make the image working, I had to modify a bit the code to check if the language is Bulgarian and add the file name:

<?php
// Showing the language flags
// remember what language is set to
$savedLanguage = $user->language;
$icon = '';

echo "<span style='margin-left:34px;'>";

foreach($languages as $language) {

// if user is already viewing the page in this language, skip it
if($language->id == $savedLanguage->id) continue;

// if this page isn't viewable (active) for the language, skip it
if(!$page->viewable($language)) continue;

// set the user's language, so that the $page->url and any other
// fields we access from it will be reflective of the $language
$user->language = $language;

// ID 1010 is of my Bulgarian (problematic language)  
if($language->id == '1010' ) {
$icon = $language->lang_flag->url . "bulgaria.png";
} else {
$icon = $language->lang_flag->url;
}
// output a link to this page in the other language
echo "<span style='margin-left:10px;'><a href='$page->url'><img src='{$icon}'></a></span>";
}
echo "</span>";
// restore the original language setting
$user->language = $savedLanguage;
?>

Has anyone had a similar case and found some more elegant solution than checking for the language ID and manually assigning the file name after $lang_flag->url ?

Share this post


Link to post
Share on other sites

@ottogal Thank you for the impressive reading. Will redo my language selector keeping the flag as the customer requested it, but will also put the language name so that it is clear what language is used. And yes, I will make sure that every language name is written as per the country standard.

Share this post


Link to post
Share on other sites
8 hours ago, MilenKo said:

keeping the flag as the customer requested it

No chance to make the customer read it, too?

Share this post


Link to post
Share on other sites

Well after your reading I went back to suggest to the "customer" who is a close friend of mine and shared the reading. So now we agreed to have the language names versus the flags or even add a dropdown box listing some countries that speak the selected languages.

Thanks again for the sharing.

  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By arnd
      Hi,
      I have a repeater with some images in it.
      As Superuser I can work with the Actions (rotate...). But my Users with lower Rights can't use this Actions.
      The same User can use the Actions on images outsite a Repeter-Field. So I think it's not a Problem of Rights-Management but from Images in Repeater Fields.
      ProcessWire 3.0.164.  Bug? Or can I manage this in the settings?
      Thanks in advance
    • By Juergen
      Hello @ all,
      I have created an inputfield with a configuration field in the backend where you can set a time format for every language (see screenshot below).

      As you can see both language values (default and German) have the default value(%R), but I have set different values, which were correctly stored in the DB (see screenshot below):

      As you can see the values are '%R' and '%r'.
      I have created the configuration inputfield like this:
      /** @var InputfieldText $f */ $languages = $this->wire('languages'); $f = $this->wire('modules')->get('InputfieldText'); $f->attr('name+id', 'timeformat'); $f->label = $this->_('Timeformat on frontend'); $f->initValue = '%R';//default value $f->attr('value', $this->timeformat ? $this->timeformat : '%R'); $this->message($this->get('timesformat')); if($languages) { $f->useLanguages = true; foreach($languages as $language) { if($language->isDefault()) continue; $f->set("value$language", (string) $this->get("timeformat$language->id")); } } $f->inputType = 'text'; $f->description = $this->_('Please enter the time format that the times should appear on the frontend in strftime format.'); $f->notes = sprintf($this->_('For example shows the time as 08:00, as 08:00 AM. You can find more examples at %s.'), '<a href="https://www.php.net/manual/de/function.strftime.php">https://www.php.net/manual/de/function.strftime.php</a>'); $f->columnWidth = 100; The important part here is:
      if($languages) { $f->useLanguages = true; foreach($languages as $language) { if($language->isDefault()) continue; $f->set("value$language", (string) $this->get("timeformat$language->id")); } } I have borrowed the code from the DateTimeInputfield (https://github.com/processwire/processwire/blob/master/wire/modules/Inputfield/InputfieldDatetime/InputfieldDatetime.module), but the field values will be always populated with the default value ('%R').
      Are I am missing something? Does anyone has experience with multilanguage fields and could help me out?
      Thanks in advance.
    • By snobjorn
      Here's my Norwegian language pack for ProcessWire. I've been adding translations over some time, to suit my own projects users needs.
      Last updated:
      2020/06/29 (June 29, 2020)
      Status:
      Not yet complete. Translations that non-superusers will see is prioritized.
      Name:
      ProcessWire-Norwegian-Language-Pack-nb-NO
      Translated using:
      ProcessWire 3.0.161 dev
      Number of files:
      202 wire files and 97 site files – ranging from just started to completely translated. The site files includes translation some free and some pro modules, see the complete list in the README.md at GitHub.
      Available at GitHub:
      https://github.com/snobjorn/processwire-norwegian-language-pack-nb-no
    • By Sten
      Hello
      Till now I hacked something with the twig template but it works no more with new PW versions so I look forward to create a module. I am working on a site in multiple languages : French, English, Italian, German, Spanish, Portuguese, Hebrew, Russian. The new posts are entered in any language with a field for language. Till now, I got twig files to get the translations with constants defined for each part of the pages.
      So I'd like to create a module to include theses files added according to the url /fr/en/...
      Have you some observations to do before I begin about the direction to take ?
      Thank you
    • By Mats
      BETA: SplashAndGrab
      https://github.com/madebymats/InputfieldSplashAndGrab
      This module attaches a search input to selected image fields that lets you search and download images from Unsplash.
      (Unsplash is a stock photo service where you can download images for free and use as you wish. No strings attached.)   
      You can search by string, colors, orientation/crop and order by relevance or time published
      I find Unsplash useful both for placeholder images when building sites but also as a time saver for editors if they don’t have any images at hand, just search, download and publish.

      Thanks to @apeisa for building the  FlickrInputField Module and @Robin S for AddImageUrls,  took a lot ideas and code from those modules.
×
×
  • Create New...