-
Notifications
You must be signed in to change notification settings - Fork 158
Localization of Stringex conversions
Note: This page applies to Stringex version 2.0 and higher
It is possible to localize the different conversions and unidecoding in Stringex, so for example "100%" becomes "100 percent" in English and "100 prozent" in German.
This goes for acts_as_url
, to_ascii
, convert_miscellaneous_characters
, convert_miscellaneous_html_entities
, and convert_vulgar_fractions
.
You can translate the different words using I18n (which is also included in Rails) or using the Stringex internal translation store. It automatically selects I18n if it's available.
I18n is the recommended way to localize Stringex conversions. You can store translations like this:
I18n.backend.store_translations :da, { :stringex => { :characters => { :and => "og" },
:html_entities => { :nbsp => " mellemrum " },
:vulgar_fractions => { :half => "halv" },
:transliterations => { "é" => "ee" } } }
First try it without localization:
"one & two".convert_miscellaneous_characters # => "one and two"
"my words".convert_miscellaneous_html_entities # => "my words"
Then translated using the different methods like you normally would:
I18n.locale = :da
"one & two".convert_miscellaneous_characters # => "one og two"
"my words".convert_miscellaneous_html_entities # => "my mellemrum words"
Ruby on Rails stores translations in config/locales/xx.yml and loads these automatically into I18n so you don't need to manually load the translations. Like this, in config/locales/da.yml:
da:
stringex:
characters:
and: og
html_entities:
nbsp: mellemrum
vulgar_fractions:
half: halv
transliterations:
é: ee
Then just set your locale and use Stringex like you normally would.
E.g. in a controller:
I18n.locale = :da
Then, in a view or somewhere else:
<%= "one & two".convert_miscellaneous_characters # => "one og two" %>
Using I18n is the recommended solution but if you want, you can use the internal backend.
# Force internal store as it will use I18n as default if it's present
Stringex::Localization.backend = :internal
# Store translations
Stringex::Localization.store_translations :da, :characters, { :and => "og" }
Stringex::Localization.store_translations :da, :html_entities, { :nbsp => " mellemrum " }
Stringex::Localization.store_translations :da, :vulgar_fractions, { :half => "halv" }
Stringex::Localization.store_translations :da, :transliterations, { "é" => "ee" }
# Set the locale
Stringex::Localization.default_locale = :en
Stringex::Localization.locale = :da
# And use it like you normally would
"one & two".convert_miscellaneous_characters # => "one og two"
"my words".convert_miscellaneous_html_entities # => "my mellemrum words"
So using I18n is easier and cleaner, but you can use the internal store for example if you don't want to depend on I18n.
Note: Some translation keys make use of matches from regular expressions to manipulate whitespace and order. Please consult the source code for StringExtensions#convert_miscellaneous_characters to see what those regular expressions look like if you need to manipulate the order differently than the English and Danish examples.
Scope: :characters
Key | Default |
---|---|
:and | and |
:number | number |
:at | at |
:dot | \1 dot \2 |
:dollars | \2 dollars |
:dollars_cents | \2 dollars \3 cents |
:pounds | \2 pounds |
:pounds_pence | \2 pounds \3 pence |
:yen | \2 yen |
:star | star |
:percent | percent |
:equals | equals |
:plus | plus |
:divide | divide |
:degrees | degrees |
:ellipsis | dot dot dot |
:slash | slash |
Scope: :html_entities
Key | Default |
---|---|
:double_quote | " |
:single_quote | ' |
:ellipsis | ... |
:en_dash | - |
:em_dash | -- |
:times | x |
:gt | > |
:lt | < |
:trade | (tm) |
:reg | (r) |
:copy | (c) |
:amp | and |
:nbsp | space |
:cent | cent |
:pound | pound |
:frac14 | one fourth |
:frac12 | half |
:frac34 | three fourths |
:divide | divide |
:deg | degrees |
Scope: :vulgar_fractions
Key | Default |
---|---|
:one_fourth | one fourth |
:half | half |
:three_fourths | three fourths |
:one_third | one third |
:two_thirds | two thirds |
:one_fifth | one fifth |
:two_fifths | two fifths |
:three_fifths | three fifths |
:four_fifths | four fifths |
:one_sixth | one sixth |
:five_sixths | five sixths |
:one_eighth | one eighth |
:three_eighths | three eighths |
:five_eighths | five eighths |
:seven_eighths | seven eighths |
Scope: :transliterations
Default unidecoder transliterations are in lib/stringex/unidecoder_data.