https://wiki.haskell.org/api.php?action=feedcontributions&user=Meteficha&feedformat=atomHaskellWiki - User contributions [en]2024-03-29T02:13:18ZUser contributionsMediaWiki 1.35.5https://wiki.haskell.org/index.php?title=Internationalization_of_Haskell_programs_using_Haskell_data_types&diff=42292Internationalization of Haskell programs using Haskell data types2011-10-03T13:20:18Z<p>Meteficha: </p>
<hr />
<div>You can use Haskell data types for internationalization.<br />
The following example is adapted from a<br />
[http://www.haskell.org/pipermail/haskell-cafe/2011-September/095715.html haskell-cafe mailing list post]<br />
by Felipe Lessa.<br />
<br />
The idea is to have a data type<br />
with all your messages, like<br />
<br />
<haskell><br />
data Message =<br />
Hello<br />
| WhatsYourName<br />
| MyNameIs String<br />
| Ihave_apples Int<br />
| GoodBye<br />
</haskell><br />
<br />
For each of your supported languages, you provide a rendering function<br />
(they may be in separate source files):<br />
<br />
<haskell><br />
render_en_US :: Message -> String<br />
render_en_US Hello = "Hello!"<br />
render_en_US WhatsYourName = "What's your name?"<br />
render_en_US (MyNameIs name) = "My name is " ++ name ++ "."<br />
render_en_US (Ihave_apples 0) = "I don't have any apples."<br />
render_en_US (Ihave_apples 1) = "I have one apple."<br />
render_en_US (Ihave_apples n) = "I have " ++ n ++ " apples."<br />
render_en_US GoodBye = "Good bye!"<br />
<br />
render_pt_BR :: Message -> String<br />
render_pt_BR Hello = "Olá!"<br />
render_pt_BR WhatsYourName = "Como você se chama?"<br />
render_pt_BR (MyNameIs name) = "Eu me chamo " ++ name ++ "."<br />
render_pt_BR (Ihave_apples 0) = "Não tenho nenhuma maçã."<br />
render_pt_BR (Ihave_apples 1) = "Tenho uma maçã."<br />
render_pt_BR (Ihave_apples 2) = "Tenho uma maçã."<br />
render_pt_BR (Ihave_apples n) = "Tenho " ++ show n ++ " maçãs."<br />
render_pt_BR GoodBye = "Tchau!"<br />
</haskell><br />
<br />
Given those functions, you can construct something like<br />
<br />
<haskell><br />
type Lang = String<br />
<br />
render :: [Lang] -> Message -> String<br />
render ("pt" :_) = render_pt_BR<br />
render ("pt_BR":_) = render_pt_BR<br />
render ("en" :_) = render_en_US<br />
render ("en_US":_) = render_en_US<br />
render (_:xs) = render xs<br />
render _ = render_en_US<br />
</haskell><br />
<br />
So <hask>r = render ["fr", "pt"]</hask> will do the right thing.<br />
You just need to pass this <hask>r</hask> around in your code.<br />
<br />
Using is easy and clear:<br />
<br />
<haskell><br />
putStrLn $ r Hello<br />
putStrLn $ r WhatsYourName<br />
name <- getLine<br />
putStrLn $ r (MyNameIs "Alice")<br />
putStrLn $ r (Ihave_apples $ length name `mod` 4)<br />
putStrLn $ r GoodBye<br />
</haskell><br />
<br />
This approach is used for internationalization<br />
in the<br />
[http://www.yesodweb.com/ Yesod web framework],<br />
except that instead of one big data type,<br />
[http://hackage.haskell.org/packages/archive/yesod-core/0.9.2/doc/html/Yesod-Message.html some type classes]<br />
are used.</div>Metefichahttps://wiki.haskell.org/index.php?title=User:Meteficha&diff=42291User:Meteficha2011-10-03T13:18:12Z<p>Meteficha: New page: I'm Felipe Lessa <felipe.lessa@gmail.com> =).</p>
<hr />
<div>I'm Felipe Lessa <felipe.lessa@gmail.com> =).</div>Metefichahttps://wiki.haskell.org/index.php?title=Pronunciation&diff=18176Pronunciation2008-01-08T20:48:50Z<p>Meteficha: </p>
<hr />
<div>Some notes for beginners on how to pronounce those strange Haskell operators etc. <br />
<br />
This is just a rough start to this page. Obviously needs more work.<br />
<br />
This can be a table with formal and informal ways of saying various operators and code snippets such as<br />
<br />
{|<br />
! Symbol<br />
! Pronounciation<br />
|-<br />
| <hask> -> </hask><br />
| maps to, to<br />
|-<br />
| <hask> -< </hask><br />
| <br />
|-<br />
| <hask> = </hask><br />
| is<br />
|-<br />
| <hask> == </hask><br />
| equals<br />
|-<br />
| <hask> => </hask><br />
| is a witness for, implies<br />
|-<br />
| <hask> . </hask> <br />
| dot (could be used anywhere, but especially in, for example, Data.Char.ord), ring, compose (for example, negate . (+1)), (silent) (for example, forall a. (Num a) => a)<br />
|-<br />
| <hask> <- </hask><br />
| drawn from, from<br />
|-<br />
| <hask> ++ </hask><br />
| append<br />
|-<br />
| <hask> >>= </hask><br />
| bind<br />
|-<br />
| <hask> >> </hask> <br />
| then<br />
|-<br />
| <hask> \ </hask><br />
| lambda<br />
|-<br />
| <hask> : </hask><br />
| cons<br />
|-<br />
| <hask> [] </hask><br />
| nil<br />
|-<br />
| <hask> () </hask><br />
| unit<br />
|}<br />
<br />
{|<br />
! Example<br />
! Pronounciation<br />
|-<br />
| <hask>f :: Int -> Int</hask><br />
| f has type Int to Int<br />
|}<br />
<br />
should we add informal, possibly bad suggestions like "then", "is", "gets"?<br />
<br />
<br />
[[Category:Syntax]]</div>Meteficha