How to change the default language and add it_IT.UTF-8 locale to locale.gen on Azure?

600 Views Asked by At

I'm trying to change the default language on the Azure machine hosting my app. Currently, the default language is set to en_US.UTF-8 and is the only one in locale.gen, but I need to configure it to it_IT.UTF-8 to support my Django project. When I start my app and the Django app execute the "import locale" command followed by "locale.setlocale(locale.LC_TIME, 'it_IT.utf8')", I get the "unsupported locale setting" error. I suspect that the Italian language is not properly configured in the system. What are the necessary steps to change the default language and add the it_IT.UTF-8 locale to locale.gen on Azure? Or any others solution? Thank you in advance for your assistance!

I created a startup.sh file in the home directory (the only persistent location) with the following commands: echo "it_IT.UTF-8" >> locale.gen locale-gen

Then, in the Azure App configuration under General settings, I specified the path /home/startup.sh in Stack Settings > Startup Command. However, when I attempted to start the machine with this configuration, the startup failed for unknown reasons.

I expected the startup.sh script to modify the locale.gen file and generate the it_IT.UTF-8 locale successfully.

1

There are 1 best solutions below

0
SiddheshDesai On

You can make use of the sample github repository here by very academy.

In your settings.py add this code to set the default language and import new languages like below:-

settings.py

"""
Django settings for core project.

Generated by 'django-admin startproject' using Django 3.1.6.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
import os
from pathlib import Path


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'o4azh=5b&5_50o$fr1h8v!^1o96*)%rri9d-wjo5icb2_!ip6d'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'lang',
    'rosetta'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'core.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'core.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'

from django.utils.translation import gettext_lazy as _

LANGUAGE_CODE = 'en'

LANGUAGES = (
    ('en', _('English')),
    ('fr', _('French')),
    ('fr', _('French')),
    ('it', _('Italy'))

)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale/'),
)

Here default language is set to English - en and rest all the languages are imported.

Now, Create 3 folders for different languages English, French and Italy in locale folder. And run the command below to populate the language translation setting in the same like below:-

enter image description here

Open your virtual environment with the commands below:-

py -m venv .venv
.venv\scripts\activate

And run the command below to populate the settings:-

py manage.py makemessages --all

enter image description here

Now, Visit locale> it > django.po file and write the text you need to translate instead of hello, I have written ciao like below:-

enter image description here

#: .\lang\views.py:10 .\lang\views.py:17
msgid "hello"
msgstr "ciao"

Now, Run the app from virtual environment locally using the command below:-

py manage.py runserver

The Web app will load like below, Change the language to French and Italian and get your desired language translated within the web app.

enter image description here

enter image description here

enter image description here

enter image description here

No, I deployed this application to Azure Web app with Python runtime and it git deployed successfully like below:-

Before deployment in **settings.py ** add allowed host to your azure web app host name like below:-

ALLOWED_HOSTS  = ['siliconwebapp87.azurewebsites.net']

enter image description here

In your scenario you can change the default language by adding it_IT.UTF-8 in your local.gen.

If local.gen does not exist create it.

sudo nano /etc/locale.gen
#add the line below in locale.gen or uncomment it if it already exist
it_IT.UTF-8 UTF-8
sudo update-locale LANG=it_IT.UTF-8
#create a startup.sh file and add the below code:-
nano startup.sh
echo "it_IT.UTF-8 UTF-8" >> /etc/locale.gen
locale-gen
chmod +x startup.sh
#run the script manually and check once:-
./startup.sh
#Once it runs successfully locally, Add the below script in your startup command in Azure app service configuration > General Settings:-
bash /home/startup.sh

Now, In your settings.py file add the following code:-

import locale
try:
    locale.setlocale(locale.LC_TIME, 'it_IT.utf8')
except locale.Error:
    pass