arrow-py / arrow-py/arrow

Storing locale with Arrow so that it is used by format

Open
#309 2 comments 0 reactions 0 assignees View on GitHub
enhancement localization
Dominant language
Python
Stars
9.1k
Forks
784
PR merge metrics
No merged PRs in 30d

Description

A great side effect of how Arrow's formatting is implemented, is that it can be used like any other type with regular string formatting, e.g.

`'My name is {name} and my birthday is {birthday:MMMM-YYYY}'.format(**myself)`

However, if I want the Arrow formatting to be in a specific locale, I have a problem. Even though I can specifiy locale with .get(), the locale is only used for parsing but not stored, and I have no way of providing arrow with the locale when calling format above.

My quick & dirty solution, which may be helpful for others with the same problem, is:

```
class LocaleArrow(arrow.Arrow):
def format(self, *args, **kwargs):
if 'locale' not in kwargs and hasattr(self, 'locale'):
return super().format(locale=self.locale, *args, **kwargs)
return super().format(*args, **kwargs)

class LocaleArrowFactory(arrow.ArrowFactory):
def __init__(self, type=LocaleArrow):
self.type = type

def get(self, *args, **kwargs):
arw = super().get(*args, **kwargs)
if 'locale' in kwargs:
arw.locale = kwargs.get('locale')
return arw

arw = LocaleArrowFactory().get(dt, locale=self.locale)
```

Would, or have you considered storing & using the locale in as part of regular Arrow?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing Arrow.format() and ArrowFactory.get(), which the issue identifies, and inspect how locale is currently passed to formatting versus parsing. Done means a locale supplied through get() is retained for regular string formatting, while explicitly provided format locales continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
internationalization
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.