Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,35 @@ define('DOSOMETHING_GLOBAL_DEFAULT_LANG_CODE', 'en-global');
function dosomething_global_init() {
global $user;

// Redirect people hitting the homepage to there local version
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

their* local version (sp)

if (drupal_is_front_page() && is_numeric(arg(1))) {

// Cannot use arg() as it doesn't return path prefixes
$path = request_path();
$args = explode('/', $path);

// Only apply logic to Global homepage.
if (!empty($path) && $args[0] != 'node') {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like some wild code to determine the home page, whyyyy? what's going on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately it had to be wild...

all of these are supporting edge cases / weird things I found. (eg: even if the URL is just / the arg 0 and 1 params still return the homepage node). The homepage is a weird thing, the comments point out which part handles what.


$country_code = dosomething_settings_get_geo_country_code();
$language = dosomething_global_convert_country_to_language($country_code);
// No matching language, serve the global page
if ($language == NULL) {
return;
}

// Get users local path prefix and verify we support it
$path_prefix = dosomething_global_get_prefix_for_language($language);
if ($path_prefix == NULL) {
return;
}

// Redirect user
drupal_goto($path_prefix . '/' . current_path());
}

// Verify we're dealing with a node edit with no translation specification in the URL
if (arg(0) == "node" && is_numeric(arg(1)) && arg(2) == "edit" && null == arg(3)) {
// Load the page node and user
Expand Down Expand Up @@ -268,6 +297,20 @@ function dosomething_global_convert_country_to_language($country) {
return NULL;
}

/**
* Converts the given language into its assigned URL prefix.
*
* @param string language
* The language to get the prefix for (eg: 'en', 'mx')
*
* @return
* The URL prefix mapped to this language
*/
function dosomething_global_get_prefix_for_language($language) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drupal has a function language_list() that returns all of the languages with their information. This query works, but I'm thinking that using the built-in function is more drupally, and it's a little less code for us to maintain. This is what language_list() returns by default, but it can return a list keyed by the field you pass in.

Array
(
    [en] => stdClass Object
        (
            [language] => en
            [name] => English
            [native] => English
            [direction] => 0
            [enabled] => 1
            [plurals] => 0
            [formula] => 
            [domain] => 
            [prefix] => us
            [weight] => 0
            [javascript] => 
        )

    [en-global] => stdClass Object
        (
            [language] => en-global
            [name] => English, Global
            [native] => English, Global
            [direction] => 0
            [enabled] => 1
            [plurals] => 0
            [formula] => 
            [domain] => 
            [prefix] => en-global
            [weight] => 0
            [javascript] => 
        )

    [pt-br] => stdClass Object
        (
            [language] => pt-br
            [name] => Portuguese, Brazil
            [native] => Português Brasileiro
            [direction] => 0
            [enabled] => 1
            [plurals] => 2
            [formula] => ($n!=1)
            [domain] => 
            [prefix] => br
            [weight] => 0
            [javascript] => 
        )

    [es-mx] => stdClass Object
        (
            [language] => es-mx
            [name] => Spanish, Mexico
            [native] => Español Mexicano
            [direction] => 0
            [enabled] => 1
            [plurals] => 0
            [formula] => 
            [domain] => 
            [prefix] => mx
            [weight] => 0
            [javascript] => 
        )

)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh thats better, for some reason i thought language list only returned a list of the languages, not all of the details. Ill switch this out!

$languages = language_list();
return $languages[$language]->prefix;
}

/**
* Get the appropriate language for the application.
*
Expand Down