Global Dev Development Q&A - DoSomething/legacy-website GitHub Wiki

The purpose of this page is to answer common problems a developer might encounter while working in our new global code base. Feel free to add more questions as you encounter them!

How do I get a users country?

User country codes are a core component of the global logic. They're attached to every request made in the form of a two letter ISO code (List of them here).

In order to read the code from the request headers, you can use dosomething_settings_get_geo_country_code.

Where does a country code come from? How do I change mine for testing?

Country codes are applied by Fastly on all non local development environments based on your location in the real world. In order to circumvent this, you'll need to use a VPN, which routes your internet traffic to another part of the world. See this doc for getting that set up https://github.com/DoSomething/phoenix/wiki/VPN-Server-for-Testing-International-Sites

For local testing, you can apply the header directly in your browser through plugins and extensions. See this guide for more info https://github.com/DoSomething/phoenix/wiki/Fastly-Global-Header-Setup#quick-setup-for-local-testing

How do I get the users language?

In order to get someones language, simply use the dosomething_global_convert_country_to_language function.

EG: dosomething_global_convert_country_to_language(dosomething_settings_get_geo_country_code());

Another option is to use the current users language, but bewared this will not work for anonymous users and you'll need the above example as a fallback.

How do I get the correct language to use for a node?

No need to panic! dosomething_global_get_language has you covered! This function will take into account authenticated users specified language, fallback for anonymous users, verification that we have a supported translation for there language, and a fallback for global if we don't. TLDR: It does all of the complicated business logic you need for nodes.

When do I use a language and when do I use a country?

Great question!

Within the platform all of our content is organized by language. So if you're trying to access a field of some sort on a node (eg: $node->some_field) you're going to need $node->some_field[$language]. All translations are also organized by language.

Externally, as explained above, we interpret users based on there country. Countries are also found in URL prefixes and administrator roles.

Where can I find a complete list of every country and associated language?

Within the :globe_with_meridians: module, checkout the dosomething_global.strongarm file. The only problem with this file is that it specifies multiple countries we don't actually support in terms of content. Currently this includes "en-gb, en-ca, fr-ca". These items are listed here to support custom login forms, not translations.

How do I grab this list in code?

Use dosomething_global_get_countries to safely retrieve the list of countries we support!