Roych Posted January 31, 2020 Share Posted January 31, 2020 Hello I'm working on some gallery and Im counting images in each album. So I have the number but now I would like to echo the text IMAGE, IMAGES next to the number of images. How would I do this? I need more than just 1 image, 2 images. In our language we also have dual so I need (in slovenian) 1 slika, 2 sliki, 3 slike, 4 slike, 5 slik ...... I have two languages ENG and SLO! Im counting images with: <a href="<?=$album->url; ?>"><div class="imgcount"><?=$album->images->count();?> IMAGES???</div></a> I hope you know what I mean. Im not a coder, not realy sure where to start here. Thank you R Link to comment Share on other sites More sharing options...
dragan Posted January 31, 2020 Share Posted January 31, 2020 @Roych Does this only apply to the first 5 numbers, or do the slovenian language rules go beyond that? If the former, you could simply create a switch/case with the image-count, and then output either slika, sliki, slike, slik in the output. Perhaps you'll need to create a translateable string variable __('foo') for this, so that you have an equivalent ENG as well (not sure which is your primary site-language). Link to comment Share on other sites More sharing options...
Roych Posted January 31, 2020 Author Share Posted January 31, 2020 Hello My primary language is Slovenian. The numbers go to 5 from then on it can always be SLIK (Images). I was thinking the translatable string variable so I can translate both languages. But not sure how to set this up. Does it have to be the php "if else" statement or is there some other aproach? I'm kinda lost here. Thank you R Link to comment Share on other sites More sharing options...
matjazp Posted January 31, 2020 Share Posted January 31, 2020 Perhaps go with "Število slik: xx" xx can be any number and works in English too. Link to comment Share on other sites More sharing options...
Roych Posted January 31, 2020 Author Share Posted January 31, 2020 Good one, this would work, but it is a little to long I would maybe still go with one word if possible. ? So number first then the word. Thank you R Link to comment Share on other sites More sharing options...
dragan Posted February 1, 2020 Share Posted February 1, 2020 $imgCount = $album->images->count(); $imgStr = ''; if ($imgCount) { if ($user->language->name === 'slo') { switch ($imgCount) { case 1: $imgStr = "slika"; break; case 2: $imgStr = "sliki"; break; case 3: case 4: $imgStr = "slike"; break; case 5: default: $imgStr = "slik"; break; } $content .= "<p>$imgCount $imgStr</p>"; } else { // not slovenian: $content .= "<p>$imgCount " . __('images') . "</p>"; } } Link to comment Share on other sites More sharing options...
MoritzLost Posted February 1, 2020 Share Posted February 1, 2020 That's actually an interesting question, because it's a flaw with ProcessWire's built-in internationalization function. A general internationalization API will support multiple plural forms, because as in your language, there's more forms than singular or plural. ProcessWire's _n function for translating singular/plural forms simply checks if the item count is exactly one or more than one and return either the singular or the plural version of the translatable string (see the source code). This works fine for English, German and many other languages, but no in languages which have more than two forms for counting items. If you still want to use ProcessWire's translation API, you'll have to handle the different cases yourself, as suggested by @dragan (though I would still use the translation API instead of hardcoding the strings in the switch statement). However, a more "general" solution that would allow you to add different language versions of your site without changing the code would be to utilize the native Gettext implementation in PHP. This will be much more work though; as far as I'm aware there's no module that provides a gettext interface for ProcessWire, so you'll either have to write one yourself or upload translation files manually. BTW I know this is probably overkill for your current situation ? But this has been bugging me for a while, it would be nice to see native support for different plural forms in the ProcessWire translation API. Might be a good candidate for a new module ... For more reading, here's a good resource for I18N in PHP. Link to comment Share on other sites More sharing options...
Roych Posted February 1, 2020 Author Share Posted February 1, 2020 @dragan Thank you for the code, I tried it but somehow didn't work for me. So I played with it it a little. It works now but not sure if everything is right as it should be. Spoiler <?php echo $imgCount = $album->images->count(); $imgStr = ''; if ($imgCount) { if ($user->language->name === 'default') { switch ($imgCount) { case 1: echo $imgStr = " slika"; break; case 2: echo $imgStr = " sliki"; break; case 3: case 4: echo $imgStr = " slike"; break; case 5: default: echo $imgStr = " slik"; break; case 101: echo $imgStr = "slika"; break; } } else { // not slovenian: { switch ($imgCount) { case 1: echo $imgStr = " image"; break; case 2: default: echo $imgStr = " images"; break; } } } } ?> Thank you R Link to comment Share on other sites More sharing options...
dragan Posted February 1, 2020 Share Posted February 1, 2020 Why did you insert so many echo()s? Remove it from echo $imgCount = $album->images->count(); and all echo() statements inside the switch statement. First we have to define a variable, before we output it ? Also, I wouldn't do a another switch for non-Slovenian language. Do something like this in the else: // not slovenian: if($imgCount === 1) { $content .= "<p>$imgCount " . __('image') . "</p>"; // Image single form } else { $content .= "<p>$imgCount " . __('images') . "</p>"; // Image plural form } That way, you can handle string translation in the PW backend, as @MoritzLost suggested: setup > languages > language > select template > submit If you look at the search template in the regular site profile, you'll also see similar examples: Link to comment Share on other sites More sharing options...
Roych Posted February 1, 2020 Author Share Posted February 1, 2020 If I remove echo's it does not show me anything. Link to comment Share on other sites More sharing options...
dragan Posted February 1, 2020 Share Posted February 1, 2020 @Roych No offence, but I guess you should develop some basic PHP know-how... There are (roughly speaking) three major template output strategies with PW Direct output is what you seem to use, i.e. constantly echo()-ing everything to the browser. In that case, On 1/31/2020 at 7:14 PM, Roych said: <a href="<?=$album->url; ?>"><div class="imgcount"><?=$album->images->count();?> IMAGES???</div></a> you would declare a variable before that language image count code block, and then use <a href="<?=$album->url; ?>"><div class="imgcount"><?=$myVariable;?></div></a> $var = ''; // init $var .= "foo"; // add stuff to that variable Since I'm using the delayed output approach in my test PW installation, my var is called $content, that's why I used it here for these quick code examples. You can of course name it whatever you want. Hope that makes sense. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now