JasperReports একটি আন্তর্জাতিককরণ (i18n) সমর্থনকারী রিপোর্টিং টুল, যা Multiple Language Support প্রদান করতে সক্ষম। এটি Properties files ব্যবহার করে dynamic content বা labels বিভিন্ন ভাষায় প্রদর্শন করতে সহায়তা করে। JasperReports রিপোর্টের মধ্যে ভাষার রূপান্তরের জন্য resource bundles ব্যবহার করা হয়, যা properties files-এ সংরক্ষিত থাকে।
এই নিবন্ধে, আমরা JasperReports-এ Multiple Language Support কিভাবে কার্যকরভাবে সেটআপ করতে হয় তা দেখব, এবং কীভাবে properties files ব্যবহার করে একাধিক ভাষায় রিপোর্টের কন্টেন্ট প্রদর্শন করা যায়।
Multiple Language Support in JasperReports
Multiple Language Support কনফিগার করার জন্য প্রধানত দুটি উপায় রয়েছে:
- Properties Files ব্যবহার করা
- JasperReports Parameters এর মাধ্যমে ভাষা নির্ধারণ করা
এই দুটি উপায় ব্যবহার করে আপনি JasperReports রিপোর্টের labels, messages, এবং অন্যান্য টেক্সট কাস্টমাইজ করতে পারেন যাতে এটি বিভিন্ন ভাষায় প্রদর্শিত হয়।
1. Properties Files তৈরি করা
প্রথমেই আপনাকে একটি properties file তৈরি করতে হবে, যেখানে আপনি বিভিন্ন ভাষার জন্য কন্টেন্ট সংরক্ষণ করবেন। প্রতিটি ভাষার জন্য আলাদা properties file তৈরি করা হয়। উদাহরণস্বরূপ, আপনি English, Spanish, এবং French ভাষার জন্য আলাদা ফাইল তৈরি করতে পারেন।
Example: English (messages_en.properties)
report.title=Employee Report
report.column.name=Employee Name
report.column.age=Employee Age
report.column.salary=Employee Salary
Example: Spanish (messages_es.properties)
report.title=Informe de Empleados
report.column.name=Nombre del Empleado
report.column.age=Edad del Empleado
report.column.salary=Salario del Empleado
Example: French (messages_fr.properties)
report.title=Rapport des Employés
report.column.name=Nom de l'employé
report.column.age=Âge de l'employé
report.column.salary=Salaire de l'employé
এই properties files-এ আপনার রিপোর্টের বিভিন্ন টেক্সট কন্টেন্ট (যেমন শিরোনাম, কলাম লেবেল ইত্যাদি) সংরক্ষণ করা হয়, এবং আপনি যে ভাষায় রিপোর্ট তৈরি করতে চান, সেই অনুযায়ী প্রপার্টি ফাইল নির্বাচন করা হবে।
2. JRXML ফাইলে Properties ফাইল রেফারেন্স করা
আপনি JasperReports এর JRXML ফাইলে resource bundle ব্যবহার করে ভাষার রূপান্তরের জন্য properties files রেফারেন্স করতে পারেন।
JRXML ফাইলের মধ্যে Resource Bundle ব্যবহার করা
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports
http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
name="EmployeeReport" language="java" pageWidth="595" pageHeight="842"
columnWidth="515" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<!-- Resource Bundle for Localization -->
<property name="net.sf.jasperreports.default.locale" value="en"/>
<property name="net.sf.jasperreports.resource.bundle" value="messages"/>
<!-- Title -->
<title>
<band height="50">
<staticText>
<reportElement x="0" y="0" width="515" height="50"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[$R{report.title}]]></text>
</staticText>
</band>
</title>
<!-- Column Header -->
<columnHeader>
<band height="30">
<staticText>
<reportElement x="0" y="0" width="170" height="30"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[$R{report.column.name}]]></text>
</staticText>
<staticText>
<reportElement x="170" y="0" width="170" height="30"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[$R{report.column.age}]]></text>
</staticText>
<staticText>
<reportElement x="340" y="0" width="170" height="30"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[$R{report.column.salary}]]></text>
</staticText>
</band>
</columnHeader>
<!-- Detail -->
<detail>
<band height="20">
<textField>
<reportElement x="0" y="0" width="170" height="20"/>
<textFieldExpression><![CDATA[$F{employee_name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="170" y="0" width="170" height="20"/>
<textFieldExpression><![CDATA[$F{employee_age}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="340" y="0" width="170" height="20"/>
<textFieldExpression><![CDATA[$F{employee_salary}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
এখানে:
- $R{report.title}: resource bundle থেকে report.title প্রপার্টি লোড করবে।
- $R{report.column.name}, $R{report.column.age}, এবং $R{report.column.salary}: এগুলি properties files থেকে কলাম নাম নিয়ে আসবে।
Important Notes:
- $R{key} সিনট্যাক্স ব্যবহার করে আপনি properties file এর মধ্যে থাকা যেকোনো প্রপার্টির মান রিপোর্টে লোড করতে পারেন।
- net.sf.jasperreports.resource.bundle প্রপার্টি ব্যবহার করে আপনি resource bundle রেফারেন্স করবেন (যেমন
messages), যেখানে messages.properties ফাইলটি রিপোর্টের ল্যাঙ্গুয়েজ অনুযায়ী কন্টেন্ট রেন্ডার করবে।
3. Java কোডের মাধ্যমে Properties File নির্বাচন করা
আপনি চাইলে Java কোডের মাধ্যমে locale পরিবর্তন করতে পারেন, এবং এটি properties file পরিবর্তন করে রিপোর্টের ভাষা কাস্টমাইজ করা যাবে।
Java Example: Setting Locale and Resource Bundle Dynamically
import net.sf.jasperreports.engine.*;
import java.util.*;
public class JasperReportLocalization {
public static void main(String[] args) {
try {
// Compile the report
JasperReport jasperReport = JasperCompileManager.compileReport("path/to/your_report.jrxml");
// Set the locale dynamically
Locale locale = new Locale("es", "ES"); // For Spanish
Map<String, Object> parameters = new HashMap<>();
parameters.put(JRParameter.REPORT_RESOURCE_BUNDLE, ResourceBundle.getBundle("messages", locale));
// Fill the report with data
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
// Export the report to PDF
JasperExportManager.exportReportToPdfFile(jasperPrint, "output/spanish_report.pdf");
System.out.println("Report generated successfully in Spanish!");
} catch (JRException e) {
e.printStackTrace();
}
}
}
এখানে:
- Locale locale = new Locale("es", "ES"): ভাষা পরিবর্তন করতে ব্যবহৃত হয়।
- ResourceBundle.getBundle("messages", locale): এটি messages.properties ফাইলের সঠিক ভাষার কন্টেন্ট লোড করবে।
4. Multi-language Support with JasperReports Studio
JasperReports Studio-এ আপনি multiple language support খুব সহজেই সেটআপ করতে পারেন:
- JRXML File তৈরি করুন।
- Resource Bundle নির্বাচন করুন (যেমন:
messages), যাতে এটি properties ফাইল থেকে localized text পেতে পারে। - Locale পরিবর্তন করতে, parameters যোগ করুন যাতে আপনি ভাষার উপর ভিত্তি করে রিপোর্ট কাস্টমাইজ করতে পারেন।
- JasperReports ব্যবহার করে multiple language support সেটআপ করার জন্য properties files একটি শক্তিশালী এবং সহজ উপায়।
- আপনি JRXML ফাইলে resource bundle ব্যবহার করে রিপোর্টের কন্টেন্ট বিভিন্ন ভাষায় রেন্ডার করতে পারেন।
- Java কোডের মাধ্যমে locale এবং resource bundle dynamically পরিবর্তন করে আপনি রিপোর্টের ভাষা কাস্টমাইজ করতে পারবেন।
- এটি internationalization (i18n) এবং localization (l10n) প্রক্রিয়ায় সহায়ক, বিশেষ করে যখন আপনার রিপোর্টে একাধিক ভাষায় কন্টেন্ট দেখানো প্রয়োজন হয়।
Read more