Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 86743cf

Browse files
add babel (part 1-3)
1 parent 4200013 commit 86743cf

File tree

6 files changed

+73
-0
lines changed

6 files changed

+73
-0
lines changed

Babel/1-locale-data/app.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from babel import Locale
2+
3+
print('# Output in english:')
4+
locale = Locale('en', 'US')
5+
print(locale.territories['US'])
6+
7+
print('# Output in danish:')
8+
locale = Locale('da', 'DK')
9+
print(locale.territories['US'])
10+
11+
print('# output lang and country:')
12+
locale = locale.parse('de_DE')
13+
print(locale.get_display_name('en_US'))
14+
15+
locale = locale.parse('da_DK')
16+
print(locale.get_display_name('en_US'))
17+
18+
print('# Output calender days')
19+
locale = Locale('en')
20+
month_names = locale.months['format']['wide'].items()
21+
print(list(month_names))
22+
23+
locale = Locale('da')
24+
month_names = locale.months['format']['wide'].items()
25+
print(list(month_names))

Babel/1-locale-data/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Babel==2.8.0
2+
pytz==2019.3

Babel/2-date-and-time/app.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from datetime import datetime, timedelta
2+
from time import time
3+
from babel.dates import format_date, format_time, format_timedelta, format_datetime, get_timezone
4+
5+
print('# Date format')
6+
d = datetime.utcnow()
7+
print('English:', format_date(d, locale='en'))
8+
print('Danish (default)', format_date(d, locale='da_DK'))
9+
print('Danish (short)', format_date(d, format='short', locale='da_DK'))
10+
print('Danish (medium)', format_date(d, format='medium', locale='da_DK'))
11+
print('Danish (long)', format_date(d, format='long', locale='da_DK'))
12+
print('Danish (full)', format_date(d, format='full', locale='da_DK'))
13+
14+
print('# Time format')
15+
t = time()
16+
print('Danish', format_time(t, locale='da'))
17+
print('Danish (am/pm)', format_time(t, 'h:mm a', locale='da'))
18+
print('Danish (secound)', format_time(t, 'H:mm:ss', locale='da'))
19+
20+
print('# Time Delta')
21+
delta = timedelta(days=6)
22+
print('USA:', format_timedelta(delta, locale='en_US'))
23+
print('Denmark:', format_timedelta(delta, locale='da_DK'))
24+
print('USA (min. month):', format_timedelta(delta, granularity='month', locale='en_US'))
25+
26+
print('# Time-zone')
27+
d = datetime.utcnow()
28+
print('English:', format_datetime(d, 'H:mm Z', locale='en_US'))
29+
print('USA/Eastern:', format_datetime(d, 'H:mm Z', tzinfo=get_timezone('US/Eastern'), locale='en_US'))
30+
print('Europe/Copenhagen:', format_datetime(d, 'H:mm Z', tzinfo=get_timezone('Europe/Copenhagen'), locale='en_US'))
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Babel==2.8.0
2+
pytz==2019.3

Babel/3-number-formatting/app.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from babel.numbers import format_decimal, format_percent
2+
3+
number = 1234.5678
4+
5+
print('# Locale number format')
6+
print('USA', format_decimal(number=number, locale='en_US'))
7+
print('Denmark', format_decimal(number=number, locale='da_DK'))
8+
9+
print('# Customer number format')
10+
print('change to 2 decimals', format_decimal(number=number, format='#,##0.##', locale='da'))
11+
print('strip to clean int', format_decimal(number=number, format='#', locale='da'))
12+
print('strip to clean float', format_decimal(number=number, format='#.##', locale='en'))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Babel==2.8.0
2+
pytz==2019.3

0 commit comments

Comments
 (0)