We should use drupal_static() in _country_get_predefined_list() too.
I've right now a case where I need to fetch the country list in different languages in the same request.
And I think the easiest - if even not the fastest way - is to temporarily change the $language global, reset the static cache and fetch the list again.
If there's a smarter way to deal with that - suggestions are welcome :)
Nevertheless we should change the static handling here - if nothing essential speaks against it.

Comments

das-peter’s picture

I refactored this again after thinking more about it.
My conclusion was that a function that returns translated strings should accept a language parameter.
Is there no coding standard / guideline rule for cases like this?

Attached patch contains now the usage of drupal_static() and the handling for the $langcode parameter.

moshe weitzman’s picture

Status: Needs review » Needs work

If you want to add caching, we need justification via benchmarks.

das-peter’s picture

Thanks for your feedback Moshe.
This is not really about caching - it's more about localization / translation.
If the $langcode parameter is added, drupal_static() probably can be removed.
But since I thought the usage of drupal_static() is encouraged, I kept it.
My intention was to be able to get the country names in a specific language - and I think every function that returns translated stuff should provide a way to define the needed language and not only rely on the $language global.

gábor hojtsy’s picture

Version: 7.x-dev » 8.x-dev

New features are added to Drupal 8.

gábor hojtsy’s picture

Also, the function seems to be a private one (it starts with an underscore) and therefore probably not intended to be used from elsewhere. I think you can do a wrapper in D7 contrib by setting the language temporarily to something else, and you can also cache results thee. For Drupal 8, if we are going to expose this to the world, we should rename the function IMHO to reflect that decision.

sun’s picture

Component: cache system » language system
Issue summary: View changes
Status: Needs work » Closed (won't fix)

This appears to be obsolete in D8 — although I could be mistaken. Please re-open if I'm wrong.