Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle month names inflection - Angular date localization

I run into an issue recently - in some parts of my angular application I have to show the date in the following format: MMMM yyyy, as well in some components of the Angular UI Bootstrap framework.

The basic issue is that in some languages the month in nominative and genitive case is spelled differently. In my case that would be Polish, Ukrainian and Russian.

As it seems by default MMMM stands for month name in genitive case, which generally makes sense.

Though I've noticed that in angular locale files for Russian and Polish language there is a property STANDALONEMONTH which, as I see, stands for month name in nominative. (though file for Ukrainian is missing that part)

Though I'm not quite sure how to take use on those. My basic suggestion would be to go with a decorator for the dateFilter.

So the question is - whether there is a standard way for month name inflection handling in angular, or a workaround that is commonly used, so that the third party libraries using the $locale would use the proper month name.

Example

date: '2016-01-20'

en-us: 
'dd MMMM yyyy' -> '20 January 2016'  
'MMMM yyyy'    -> 'January 2016'    

uk-ua: 
'dd MMMM yyyy' -> '20 січня 2016'    // genitive case - it is fine
'MMMM yyyy'    -> 'січня 2016'      // wrong: should be 'січень 2016'

pl-pl: 
'dd MMMM yyyy' -> '20 stycznia 2016' // genitive case - it is fine
'MMMM yyyy'    -> 'stycznia 2016'    // wrong: should be 'styczeń 2016'

As you see - for Ukrainian and Polish the name of the month should have a different ending in those two cases. As well for Polish and Russian there is a month name in proper case in angular locale files (STANDALONEMONTH). Though I'm not quite sure they are used anywhere - was not able to find usage, or maybe I am missing something.

like image 542
danyloid Avatar asked Oct 30 '22 09:10

danyloid


1 Answers

I couldn't find anything in the built-in locale service when I was working on a similar issue some time ago in Angular 1.3, so I have solved this by manually creating a list with translations in proper case and referencing those by month number, something like {{monthNamesNominative[monthNumber]}} where var monthNamesNominative = ['Январь', 'Февраль', ...]. The added benefit here is that you have direct control over the string and don't have to do extra jumps if you need a slightly different capitalisation.

Having said that, locale service in Angular 1.5 has the LLLL token with month name in nominative case and it appears that four days after you asked this question the bug in Angular that prevented its correct use was fixed, and from version 1.5.1 onward using it instead is probably better.

like image 109
n1313 Avatar answered Nov 12 '22 13:11

n1313