mootools / mootools-core

MooTools Core Repository
https://mootools.net
2.65k stars 510 forks source link

String.prototype.capitalize() isn't corretly capitalizing words with accents #2680

Closed andreynering closed 9 years ago

andreynering commented 9 years ago

Some languages like spanish and portuguese have words with accents. MooTools isn't correctly capitalizing these words:

'música'.pluralize() //MúSica

An example in JSFiddle: http://jsfiddle.net/3xLjf6t9/

SergioCrisostomo commented 9 years ago

@andreynering I feel your pain! My name would actually fail that method.

The question is should the Core method work for all languages? just latin-germanic languages? or just valid letters from CSS class names?

A solution for latin-germanic languages could be:

capitalize: function(){
    return String(this).replace(/([^A-zÀ-ú]?)([A-zÀ-ú]+)/g, function(match, separator, word){
        return separator + word.charAt(0).toUpperCase() + word.slice(1);
    });
},

and a spec:

expect('a música é universal'.capitalize()).toEqual('A Música É Universal');

tried this ^ on Travis and works good.

@andreynering: I started a thread on Portuguese Stackoverflow with this fix. welcome to join there :smile:

arian commented 9 years ago

It's really depends what this method is meant for. Afaik this is mostly meant for regular simple programming problems, and not for internalization. If you start doing that, then there are at least 101 other cases we would need to cover, and the question is where we should draw the line.

ibolmo commented 9 years ago

Indeed, but I was wondering if we could do something like [^\d\D] (this was obviously rushed) but the idea is to separate by non-alpha(unicode)

On Thu, Nov 27, 2014 at 2:05 PM, Arian Stolwijk notifications@github.com wrote:

It's really depends what this method is meant for. Afaik this is mostly meant for regular simple programming problems, and not for internalization. If you start doing that, then there are at least 101 other cases we would need to cover, and the question is where we should draw the line.

— Reply to this email directly or view it on GitHub https://github.com/mootools/mootools-core/issues/2680#issuecomment-64826848 .

andreynering commented 9 years ago

Actually, capitalize() already correctly capitalize words with dashes: http://jsfiddle.net/h1g0g261/

@SergioCrisostomo Thank you! I'm using your solution. =)

SergioCrisostomo commented 9 years ago

We decided yesterday we will leave this method as it is since we cannot include all cases in a simple solution that is not expensive for performance. Also this method is used mostly for internal purposes and works perfect for such.

@andreynering happy my idea was usable! Cheers

andreynering commented 9 years ago

Thank you for the feedback.

Andrey Nering Em 15/01/2015 11:29, "Sergio Crisostomo" notifications@github.com escreveu:

We decided yesterday we will leave this method as it is since we cannot include all cases in a simple solution that is not expensive for performance. Also this method is used mostly for internal purposes.

@andreynering https://github.com/andreynering happy my idea was usable! Cheers

— Reply to this email directly or view it on GitHub https://github.com/mootools/mootools-core/issues/2680#issuecomment-70085142 .