Skip to content
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

Introduce run-time language switching #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
elm-stuff
node_modules
out
example/src/Translation
linter-logs
build
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ dist: $(ELM_FILES)
elm-make src/Main.elm --output dist/elm.js


gentest: dist
cd example && node ../extractor.js -l De,En -s --root src

imptest: dist
cd example && node ../extractor.js --format CSV --language En --importOutput src/ --import languages/en.csv

test: ## Run tests
./node_modules/elm-test/bin/elm-test

Expand Down
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,75 @@ elm-i18n-generator --format PO --language De --import export.po
Results in the same `import/De/Translation/Main.elm`
as in the [CSV example](#import-generate-elm-source-code-from-csv).


#### Generate Elm source for switching Language during run-time

Pass all the languages you want to switch between as a comma-separated list.

```bash
elm-i18n-generator --language De,Pl,En,Fr --genswitch
```

#### Migrate legacy symlinked translations to switchables
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should maybe not just yet call it legacy. I believe that both options could reside side-by-side with advantages for each approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we need to implement this in a way that does not break existing implementation unless they use new cli flags.


Remove the legacy symlink
```
rm src/Translation
```

Export each of your languages to CSV temporarily
```
mkdir languages
elm-i18n.generator --format CSV --language En --root Translation/ --export --exportOutput languages/en.csv
```

Then, import them again.
```
elm-i18n-generator --format CSV --language En --importOutput src --import languages/en.csv
```


Finally, generate the switch
```
elm-i18n-generator --language De,En --genswitch --importOutput . --root src
```

After this your Project should look like this:

```
project
└── src/
├── Main.elm (e.g. imports Translation.Main)
├── View.elm (e.g. imports Translation.View)
└── Translation
├── Main.elm (module Translation.Main)
├── View.elm (module Translation.View)
├── Main/
│ ├── De.elm (module Translation.Main.De)
│ └── En.elm (module Translation.View.En)
└── View/
├── De.elm (module Translation.Main.De)
└── En.elm (module Translation.View.En)
```

Use the `Language` type to dynamically translate your websites:


```
import Translation exposing (Language(..))

state =
{ language = Language
}

render state =
div []
[ text (Translation.Main.greetingWithName state.language "Leonard")
, text "and in German:"
, text (Translation.Main.greetingWithName De "Leonard")
]
```

## Advantages

+ Each build of your app only contains one language.
Expand Down
Loading