-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enhance Inflector helper with ascii function #464
Conversation
* @param array $replace the characters to be replaced by spaces. | ||
* @return string the translated | ||
*/ | ||
public static function ascii($string, $replace = array()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need this $replace
because it does something that has nothing to do with ascii
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it is true, but when you translate something like: I'll be back
, you get Ill be back
which will make a wrong slug: Ill-be-back
instead of I-ll-be-back
if we use replacement tokens
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why I'll be back
becomes Ill-be-back
? Isn't '
an ASCII char already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, I get your point, that should be work of the slug function.
…moved dependency on ascii function
* upstream: Fixed build break. Fixes issue yiisoft#194: Added Application::catchAll. added missing default to getValue in boostrap tabs \yii\widgets\Menu improvement Fixes issue yiisoft#467: allow view file to be absent as long as the themed version exists. better auto scrolling. Fixes issue yiisoft#446: automatically scroll to first error.
*/ | ||
public static $transliteration = array( | ||
protected static $transliteration = array( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why turning this into protected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imo they should all be customizable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also transliteration prop have incorrect map. Please check this http://iamseanmurphy.com/creating-seo-friendly-urls-in-php-with-url-slug/ .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have inspired my self on that link, how come is incorrect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tonydspaniard For example there is no rule for cyrilic а
. Also for example rule '/ъ|ь|Ъ|Ы|Ь/' => ''
incorrect because Ы
should be Y
. If you inspired by this link we should take these rules carefully ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tonydspaniard I think its good idea to make some kind of converter to automatic convert rules from link to this format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @cebe, working on this
* upstream/master: (91 commits) fixed init.bat paths renamed backstage → backend Fixed test break. Response WIP coding style fix. [1] Redone missing code. [2] Added empty line at the end of file [3] Removed exception. Updated code style. braces on the same line for control statements. Removed unused columsn from find constraint sql. Fixed typo. Added extra schema check for when a foreign table is not in the same schema. Updated indentation to conform to other classes. Multilevel Items Fixes issue yiisoft#514. Removed the config setting that should not have been commited. Removed false exception catching. Removed custom pgsql PDO and added defaultSchema as public property. CS fixes. Minor refactoring of DbMessageSource. Response WIP Minor refactoring of t(). Added Jsonable support. Fixes yiisoft#13. Implement DB message source. Yii::t() minor fix. ...
@tonydspaniard as we are on PHP 5.4 now, can you check if/whats obsolete here now? |
@cebe transliteration is now available in 5.4 intl: http://www.php.net/manual/en/class.transliterator.php |
We're creating a fallback for English only where transliteration isn't needed. |
I understand it, so why not to make public static function slug($string, $replacement = '-')
{
if(extension_loaded(intl)) {
$options = "Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;";
//best options should be evaluated, just a quick copy-paste
$string = transliterator_transliterate($options, $string);
$string = preg_replace('/[-\s]+/', '-', $string);
return trim($string, '-');
} else {
$map = static::$transliteration + [
'/[^\w\s]/' => ' ',
'/\\s+/' => $replacement,
'/(?<=[a-z])([A-Z])/' => $replacement . '\\1',
str_replace(':rep', preg_quote($replacement, '/'), '/^[:rep]+|[:rep]+$/') => ''
];
return preg_replace(array_keys($map), array_values($map), $string);
}
} |
* upstream: (2012 commits) doc fix. Changed the signature of `urlCreator` and button creators for `yii\gridview\ActionColumn` parameter adjustment. The signature for `yii\gridview\ActionColumn::urlCreator` is changed - the `$action` parameter is moved to the first Fixed the signature of Schema::findUniqueIndexes(). reverted yiisoft#1598 and added a test for it Fix wrong array index in unique indexes for MySql Making accesors public Get DB unique indexes information Fixes yiisoft#1610: `Html::activeCheckboxList()` and `Html::activeRadioList()` will submit an empty string if no checkbox/radio is selected Gii should keep horizontal layout Documentation at "yii\authclient" updated. Doc comments at "yii\authclient" updated. Auth clients "Choice" doc comments updated. Auth clients "Choice" widget javascript advanced. Bootstrap's dropdown encodes also trailing caret tag Auth clients "Choice" widget markup updated. Gii should keep horizontal layout extended from codeception testcase, added docs Auth clients for Facebook, GitHub, LinkedIn added. ... Conflicts: framework/yii/helpers/BaseInflector.php tests/unit/framework/helpers/InflectorTest.php
After reviewing and checking lots of sources out there, I couldn't find a really good and simple method to transliterate text. I have added the TransliteratorHelper class for you guys to review it. It could be that the data to transliterate is at the wrong location Edit: TransliteratorHelper fallsback into |
I just reviewed the code. I think it's a bit too much including It seems to me @iJackUA's approach is good enough. What do you think? |
Yeah... I thought about transliteration/data was too much, but was the best approach so far. I will rollback to previous and use @iJackUA's approach updating Thanks @qiangxue |
if I understand correctly
makes much more right now - it transliterate any language (at least I have tried with Russian) to Latin characters in my test with string btw should there be an option for lowercasing the string in |
Lowercasing it is a good idea. |
@iJackUA I am going to try to include the Cyrillic reference but do not expect great results as with the helper i have to remove. For those interested on using a more robust transliteration fallback, please check (done versions for both Yii1 + Yi2): https://github.com/2amigos/transliteration-helper @samdark I guess add a bool parameter to the function won't hurt. |
* upstream: (21 commits) Fixes yiisoft#1643: Added default value for `Captcha::options` Fixes yiisoft#1654: Fixed the issue that a new message source object is generated for every new message being translated Allow dash char in ActionColumn’s button names. Added SecurityTest. fixed functional test when enablePrettyUrl is false. fixed composer.json minor doc fix. Fixes yiisoft#1634: Use masked CSRF tokens to prevent BREACH exploits Use better random CSRF token. GII unique indexes avoid autoIncrement columns updated debug retry params. Added sleep(). Added unit test for ActiveRecord::updateAttributes(). Fixes yiisoft#1641: Added `BaseActiveRecord::updateAttributes()` Fixed yiisoft#1504: Debug toolbar isn't loaded successfully in some environments when xdebug is enabled Mongo README.md updated. Fixes yiisoft#1611: Added `BaseActiveRecord::markAttributeDirty()` Number validator was missing Fixes yiisoft#1638: prevent table names from being enclosed within curly brackets twice. Unique indexes rules for single columns into array ...
@qiangxue please review new approach. Tested both with Updated char map and fallback transliteration approach. |
]; | ||
return preg_replace(array_keys($map), array_values($map), $string); | ||
// ensure UTF-8 and remove invalid UTF-8 chars. | ||
$string = mb_convert_encoding((string) $string, 'UTF-8', mb_list_encodings()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really needed? Or is it correct to do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to be very defensive always on code. Maybe it is not needed. I can remove it.
* upstream: Fixed CSRF token masking issue. improved error message of calling invalid scope method. Fixed repo URL Fixes yiisoft#1650: Added Connection::pdoClass. code style fix. added changelog codestyle fix improved checkIntegrity method Modified extension guidlines fix sphinx command signature fixed bug with forgotten param, fixed behavior for one table integrity fixed sequence reset added postgresql features to reset seq/check integrity
enhance Inflector helper with ascii function
Thanks! |
closes #364
Edit:
Seems to me that transliteration without PECL or the new Transliterator class (PHP >= 5.4), is something that a simple array won't do.
I think is ok on the first round, but if we wish to adopt transliteration the right way, we should consider some good rework on this matter.
References:
https://drupal.org/project/transliteration
http://php.net/manual/es/transliterator.transliterate.php
https://doc.wikimedia.org/mediawiki-core/master/php/html/UtfNormal_8php_source.html