Multi-Language
Language Switching in Flutter Without Third-Party Packages
Language switching is essential for making your app globally accessible.
Switching Languages Using JSON Files
The advantage of this method is that all language files are stored in JSON format, which can be downloaded from a server. This reduces the app's initial download size from the app store.
This approach is commonly used in mobile apps. However, it is not the method used in this particular website. I will introduce a different language-switching approach for Flutter web in the next section.
Create JSON Templates

See how I set up JSON files in the image. I have four languages: English, Japanese, Korean, and Vietnamese.
Here is the content inside the en.json file:
Here is the content inside the vi.json file:
Similarly, create JSON files for other languages. You can see that we use the keys of the elements in the JSON file to identify the corresponding language.
Before going into details, I will explain my idea:
I will read the content from the JSON file, then use the shared_preferences library https://pub.dev/packages/shared_preferences to save the read content into the device's memory.
When using it, we only need to retrieve it from memory. When switching languages, we repeat this process from the beginning.
First, we will customize the shared_preferences library a bit. This is how I separate libraries from my main project.
The advantage of this approach is that it makes it easy to replace libraries in the future if they are no longer supported and minimizes the complexity of integrating the library into the project.
I have explained this in my article about package management
I will omit unnecessary functions and keep only the main ones: setup, read, and write.
After setting up the functions to store the current language in the device's memory, we move on to the setup step to read and retrieve the current language from the JSON file.
Assuming that all your language JSON files are stored in the "assets/i18n/" directory and you have declared them in pubspec.yaml.
Initialize a global variable in the project:
Next, set up the list of languages that will be displayed in the application:
Suppose I already have a list of languages specified for the application as follows:
Add the JSON file to the assets/i18n directory and declare it in pubspec.yaml:
Change Language
This function automatically saves the selected language to device storage.
Create a Translator
We will create a separate class OurInterpreter to handle translations retrieved from the JSON file:
Usage
This approach is ideal for supporting multiple languages. It offers flexibility since translations can be fetched from a JSON file on a server. However, it comes with the trade-off of additional loading time.
Simple Language Switching with Inline Data
This method avoids loading language files from JSON, making it ideal for web applications. Let's get started:
We will store all language translations in a Dart file as a Map, like this:
Similarly, we create maps for other languages.
Next, we use the intl package (https://pub.dev/packages/intl) as the main localization library for the app. This library allows us to:
Change the language of built-in Flutter dialogs, which default to English.
Format dates, times, and currency according to the selected language.
Idea
The approach is simple:
Store all translations in a single
Map.Retrieve a word or phrase by its key whenever needed.
Use this code snippet to change the language to English:
Usage:
This method is much simpler than the first one, but it is only suitable for applications with static data.
Three Tips for Quick Language Switching
Using the json-translator Tool
json-translator ToolThis is the first tool I used to automate language translation, saving a lot of time and effort. You can refer to the documentation here: json-translator on npm.
How to Set Up?
Step 1: Install npm
On MacBook M1:
Step 2: Translate JSON files into multiple languages
Refer to the official documentation: json-translator on npm.
Open the terminal with Admin privileges.
Run the following command:
If you encounter the error
zsh: command not found: jsontt, fix it by running:On Windows, grant permission to run PowerShell scripts:
How to Use?
Step 1: Run the translation command
The translated JSON file will be saved in the same directory as the original file.
Example commands:
Usage Tips:
Use
Ctrl + Shift + Cto copy the folder path.Press
Spaceto select the target language.
While this method helps automate translation, manual adjustments are still necessary, especially for technical terms, as machine translations may not always be accurate.
Using ChatGPT for Bulk Language Translation
The emergence of ChatGPT has significantly reduced the workload of tedious tasks, allowing us to engage with the world more effectively through language.
In the past, there were concerns about the accuracy of Google Translate. However, ChatGPT now provides much more natural and comprehensible translations, especially for technical documents.
How to Use ChatGPT for Bulk Translation
Here’s the command I frequently use to have ChatGPT translate multiple terms at once. This method is ideal for solo developers working on their projects:
After receiving the translated JSON data and code snippet, simply copy and paste them into your JSON files and the OurInterpreter class.
Generating Code from JSON or Map to Reduce OurInterpreter Declaration Time
OurInterpreter Declaration TimeThe idea is simple: I asked ChatGPT-4o to generate a Dart function that reads values from a JSON file and automatically creates an OurInterpreter class.
After receiving the generated code, I made a few adjustments to ensure accuracy and compliance with my preferred structure.
Below is the function I developed, which you can use as a reference. The key to leveraging AI effectively is to create tools that automate everything for you. If you want to skip the complexity and use it quickly, check out my package on pub.dev: https://pub.dev/packages/my_lang
Last updated