Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to automate the instalation of drupal in a language other than english #704

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
46 changes: 46 additions & 0 deletions .ddev/commands/web/setup-language
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
Copy link
Member

Choose a reason for hiding this comment

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

I see it a bit fragile to do it with string manipulations.
Why not doing this inside Robo?
Then, as Drupal is bootstrapped, we can simply use the API to achieve this. Then we can a drush cex and a git commit even.

https://gorannikolovski.com/snippet/how-programmatically-add-language#:~:text=Sometimes%20you%20have%20to%20add,)%3B%20%24language%2D%3Esave()%3B

Also

<?php

// Ensure this file is executed in the context of a Drupal bootstrap.
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;

if (!drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL)) {
    drush_log('This script must be run in the context of a fully bootstrapped Drupal site.', 'error');
    exit(1);
}

// Define the language code of the new language to be installed.
$new_language_code = 'es'; // Spanish (change 'es' to the desired language code).

// Check if the language already exists.
$existing_language = \Drupal::languageManager()->getLanguage($new_language_code);

if ($existing_language) {
    drush_log(dt('The language @lang_code already exists.', ['@lang_code' => $new_language_code]), 'warning');
    return;
}

// Create and save the new language.
ConfigurableLanguage::create([
    'id' => $new_language_code,
    'label' => 'Spanish', // Change this to the appropriate language name.
    'weight' => 0,
    'locked' => LanguageInterface::LOCKED
])->save();

drush_log(dt('The language @lang_code has been added.', ['@lang_code' => $new_language_code]), 'success');

// Rebuild the cache.
drupal_flush_all_caches();

drush_log('Cache has been cleared.', 'success');

(AI, as-is)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see it a bit fragile to do it with string manipulations.

Yep true. Maybe we can set this feature as experimental and use with caution, by default for English it does nothing as it exits before doing any string manipulation.

@AronNovak The reason for leaving this out of Robo is because this approach install all the content in the language of choice. Adding a new language and doing a config export will leave a lot of content created English.


## Description: Configures drupal to install language
## Usage: setup-language LANGCODE
## Example: "ddev setup-language es Spanish"

if [ $# -ne 2 ]; then
echo "Usage: $0 <langcode> <language name>"
exit 1
fi

langcode=$1
langname=$2

# The site is configured in english. Nothing to do here.
if [ "$langcode" == "en" ]; then
exit 0
fi

directories=(
"config/sync"
"web/modules/custom/server_default_content"
)

# Find and replace "langcode: en" with "langcode: $langcode"
for dir in "${directories[@]}"; do
if [ -d "$dir" ]; then
grep -rl "langcode: en" "$dir" | while read -r file; do
sed -i "s/langcode: en/langcode: $langcode/g" "$file"
done
fi
done

TARGET_FILE="config/sync/language.entity.${langcode}.yml"

# Make sure the language selected exists. If not, reuse Spanish as source.
if [ ! -f "$TARGET_FILE" ]; then
mv config/sync/language.entity.es.yml "$TARGET_FILE"

sed -i "s/id: es/id: ${langcode}/" "$TARGET_FILE"
sed -i "s/label: Spanish/label: ${langname}/" "$TARGET_FILE"
sed -i "s/weight: 2/weight: -1/" "$TARGET_FILE"

sed -i "s/en: ''/en: en/" config/sync/language.negotiation.yml
sed -i "s/es: es/${langcode}: ${langcode}/" config/sync/language.negotiation.yml
fi
4 changes: 4 additions & 0 deletions .ddev/config.local.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ hooks:
# Private files directory.
- exec: mkdir -p /var/www/private

# Default instalation languge: English. Change this to different langcode
# to prepare a drupal instalation in a different language.
- exec-host: ddev setup-language en English

# Create a private key for Two-factor Authentication if not yet added.
- exec-host: ddev set-tfa-key

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,15 @@ To import the config translations:
- Run `ddev drush config:export`
- Review & commit the config changes

#### Changing main language

By default, the Drupal starter setup installs Drupal in English. To change the
default language to something other than English, you need to modify the
.ddev/config.local.yaml.example file. Specifically, update the file to run the
`setup-language <your_language_code> <language name>` command, for example
`setup-language fr French`. This adjustment will configure Drupal to use
the specified language (French) instead of English.

## Two-factor Authentication (TFA)

TFA is enabled for the Administrator and Content editor users.
Expand Down