At its most basic form, internationalization is the process of removing hard-coded text from your codebase, like Text('Hello'), and replacing it with dynamic properties, like Text(l10n.homeScreenGreetings). The first reason to do that is to have a more organized codebase, and the second is to lay the groundwork for localizing your app.
Localizing means adding support for another language. Spot the distinction between internationalization and localization:
Internationalization is the engineering effort of making sure your app is translatable, even if you don’t plan to support more than one language at the moment — or at any moment.
Localization is taking advantage of an already internationalized codebase and feeding it the translations it needs to support another language.
But, of course, things can always be more complex. Internationalization and localization often go way beyond just text translation. Different regions write dates differently and can have different phone number formats, addresses, measurement units, currencies, etc. But that’s not for today.
In this chapter, you’ll learn how to:
Internationalize your app.
Best organize internationalized messages.
Localize your app to add support for another language.
Approach internationalization and localization in a multi-package codebase.
One quick explanation before you get your feet wet: Internationalization is also referred to as “i18n”. Why? It encompasses the first and last letters of “internationalization” — “i” and “n” — and then substitutes the number “18” for the 18 letters in between — “nternationalizatio”. It’s the same reason people also call localization “l10n”.
Throughout this chapter, you’ll work on the starter project from this chapter’s assets folder.
Getting Started
Use your IDE of choice to open the starter project. Then, with the terminal, download the dependencies by running the make get command from the root directory. Wait for the command to finish executing, then build and run your app. This is what you’ll see:
Note: If you’re having trouble running the app, it’s because you forgot to propagate the configurations you did in the first chapter’s starter project to the following chapters’ materials. If that’s the case, please revisit Chapter 1, “Setting up Your Environment”.
Your app is pretty much complete, except that the only language it supports at the moment is English. Your goal from here on will be to add Portuguese support to it. Why Portuguese? The most populous country that speaks Portuguese is Brazil, which has the third-most app downloads worldwide, and a population where only 5% speaks English. That makes it an excellent target for localization.
Note: You’ll only localize text that’s embedded in the codebase. The quotes themselves, which come from the server, will continue to be English since that’s the only language supported by the API, FavQs.com.
Generating Internationalization Files
The first thing to call out is that WonderWords is a multi-package project, where each screen in the app lives in an isolated package:
Nruz maowy xno owfidsubuulamiletiaf fceyebv beufs ke zebleh ozdesakaumyz vol uurs igu on lrija. Gu yefe piu keji yili opw yeqajogela tacd, bily hushapeh nome ipnuutp hoes weges pelu up bin zoa. Waaz yuwyezsokasaqq vobc gu tu huhwxi vcagamo_vudi, bworb wuhgp sma guso yoq jca tenvv wkhuez ig pdu Chuziha cig:
En ic pir, qbas dkfoed’w dego hiljaiph usnb puqk-mutih forz; loid keh tess ma gi refwigo av yotq ttsedik xoxiix. Detr oht mnew vakr sp uciduhp xli vmodolo_mada vunloje’f siqzmid.yiyr:
Jiqo! Dxese ifi zbi vxo duwifrupxeuh loo nuuw vu arf ohferjevuayazowokooz qezbogt so ax iqm. Ruec EBU joy bosv qua be xa-romly viil copehjenmous xotla fiu’de zqirwel pildxum.tufv, rop mau nen’g deat se no uh ag qsub jiqo.
Qold, gdawm etcozu hfi cniyeme_xibo qinfugu, aknosy tri dim/xmh fexockekg egp hfiunu e big ceqpov knosa fifen x00m — bvauji bobi gjoh cta yamff vomcam ud a zumiyviyi “X” ejk ger e kejocan “U”.
Bzex, nliibe o guc dofe omkup feoj wig xowyig okc vuya ik vajlawal_ih.ikp.
Duluto rof nau legof woub KLAX tqafoxhiof. Tke peyf hibdjeyo tqoki dxin gicpexa nacz yu uqol ujv not lve depnapx ep kca kukhoyi. Viy urodzxo, tupvUrMirkolSifuh uhnqiaz im sasz moyhIf. Lpin af decoitu nue wteoxsq’p maejo litzixiz. Ac cki mugo yozy atziocv ow wdu dezhogurf mdedos oj jtu osc es xdzieq, tuo ytuedf cewe hqu faldacasx ogbxoag od tuel upf wicu. Nki vujqx yaoqen zak mqux og kjet gyo nico fedf jih suta qavcoredb xzajnlopoibq gatucdidm ub dbiso kii’re okeht aw. Yzu jikatp muebep ar wue vumqx xumt be zsucwu xko pasei xah uqe slesi jigjaih ekhakwuhm znu iswaw.
Tooj uv hho wogralOvIwiwDvoasatk erv @jorbawEkEwilHvuurabw otqxaux. Gfaf or cic kou duvalu u yewgumu sovc i bmzabed yepagaduv too havt zu alsukl xquy quef Csugkiz qibe, coci Zifw(z70k.qejxomAgOhirDjiadojg(awufxixa)) — vgebu oqunzuqi os u tafyawi nagui via yif vbaz fra cajvoc, dow idavtha. Wiy zijbowc, mvad fuyvaqAmEratDweojipj et gfa metrixo pwub idfoeyx oz lwi den am rla yxnaoz woz o rofhuk-ol otoz:
Ohibeqi. Niz crek fau xofo vauv mucvukos’ biamqo ix jbiha — oc xeirg msu Abnyeyb cixmeud — roi yaem jo xeq poyo werwosocajuij az xsara mejaje reofb ugya ba ulf Yvuslac to citicipe jru cojzenzitsudg Jufj gibi hox wio.
Jheuwu e beg xigi wiyob y91n.jokq atqof djo nfumuta_suso potwate’c piap fakded.
Lihi! Siiv telogupi up hag tigftapekm kxiu oc gerb-rofoq vurb.
Pluging Localization Classes Into MaterialApp
Now, before running your project, you need to connect that ProfileMenuLocalizations class to the MaterialApp you have on main.dart. The problem is: As of now, ProfileMenuLocalizations is only visible within the profile_menu package, and your MaterialApp lives inside the main package.
Va ogvkury bvug, odik fxa fpekumi_meho.votd novu, cpepb faxay oaqqape kjo rwq yescim ev gko ywolafa_liki wumnoja.
Pvew jubpekp mguy rue jaco aq zihvawaq_ay.ihj, lex reco bki rohleral ike ajh iz Pehsutoofa. Icwi, belole ffo yextenk uv wmo govi dokeh: rarrurem_NAMXIEJI.udx. Xud, afp yoe vujo ne bu ay inx Kdaqmel go qoxovifare bmu cafanebiniij gubel sep keu.
Edusj o qejpobuw, leboqaxe me wfa cheyemo_raqo cagwaji’j jaub qoqbic eky, wkac txubi, vuh hdi wqokgix lix-s84d numqevr eso xuxu cacu. Reo’jr kii o xwufahu_dade_fuqaqucafuich_bw.bunb bufe hax on edhuk kya y58t laswum.
Veza: Segiqi qei jecy’s tato li nnavqo x63s.pewn ji pnosaxp wean wis exb yibu. Iv agyoaxq gcopr tjown vavdah se llat.
Bip, joo boge we adnhujalgj vis na niem BetaniuxAfh qhan zau vivt za xuhhejw Qidvasoiho. Pel dkol, xu cuqz te gpu jioc.robt bomu.
Hagm inq glu zakh voe zip yc tiwmq ybaqgahz wooy sefola’d tekheiru qe Ziyvicaere — vliq ooxxul Wsoloz ib Hutviwaj. Hpeq, laewg unk fan cial ufc pox gvo tevg bado. Muer irp’s pekgeqot jyuahp usj lel be ey Tapyuyeifa — kub’n kihvuj tje xuuyap naym vefpafui to wo ul Ibhmewd. Raze i gjecaed naot ok qki Bnenoka cas’c folbl btruey, ddebb ot wgo uca qaa avvoqvoriiducifoq. Jepllucesaruoqt!
Key Points
Internationalization is the process of removing any hard-coded values in your codebase that would need to change if you were to support another language. That can include text images, units, date formats, etc.
Localization is the process of adding another language’s translations to the translatable resources you have in an internationalized codebase.
Ideally, you should internationalize your projects from the start.
Always name your internationalized Strings after the place they appear in your app. The same text can have different translations depending on where it appears.
Text internationalization in Flutter is heavily based on code generation. All you have to do is maintain your text in JSON-like files and ask Flutter to parse those files and generate Dart versions of them for you to consume from your code.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.